Home > Web Front-end > JS Tutorial > body text

JavaScript method to return the number of milliseconds from January 1, 1970 to a specified date based on Universal Time UTC()

黄舟
Release: 2017-11-08 14:10:38
Original
3202 people have browsed it

Definition and Usage

The UTC() method returns the number of milliseconds from January 1, 1970 to the specified date based on universal time.

Syntax

Date.UTC(year,month,day,hours,minutes,seconds,ms)
Copy after login
ParametersDescription
year Required. A four-digit number representing the year.
monthRequired. Integer representing the month, ranging from 0 ~ 11.
dayRequired. An integer representing the date, ranging from 1 to 31.
hoursOptional. An integer representing the hour, between 0 and 23.
minutesOptional. An integer representing minutes, ranging from 0 to 59.
secondsOptional. An integer representing seconds, ranging from 0 to 59.
msOptional. An integer representing milliseconds, ranging from 0 to 999.

Return Value

Returns the number of milliseconds from the specified time to midnight GMT on January 1, 1970.

Explanation

Date.UTC() is a static method because it needs to be called using the constructor Date() instead of Called through a Date object.

The parameters of the Date.UTC() method specify the date and time, which are both UTC times and are in the GMT time zone. The specified UTC time is converted to milliseconds so that it can be used by the Date() constructor and the Date.setTime() method.

Example

Example 1

In this example, we will get the number of milliseconds from 1970/01/01 to 2005/07/08 based on universal time:

<script type="text/javascript">

var d = Date.UTC(2005,7,8)
document.write(d)

</script>
Copy after login

Output:

1123459200000
Copy after login

Example 2

Now, we will transform the above example so that the output is converted to years:

<script type="text/javascript">

var minutes = 1000 * 60
var hours = minutes * 60
var days = hours * 24
var years = days * 365
var t = Date.UTC(2005,7,8)
var y = t/years
document.write("It&#39;s been: " + y + " years from 1970/01/01")
document.write(" to 2005/07/08!")

</script>
Copy after login

Output:

It&#39;s been: 35.62465753424657 years from 1970/01/01 to 2005/07/08!
Copy after login

The above is the detailed content of JavaScript method to return the number of milliseconds from January 1, 1970 to a specified date based on Universal Time UTC(). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template