Home > Article > Web Front-end > JavaScript method to return the number of milliseconds from January 1, 1970 to a specified date based on Universal Time UTC()
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)
Parameters | Description |
year | Required. A four-digit number representing the year. |
month | Required. Integer representing the month, ranging from 0 ~ 11. |
day | Required. An integer representing the date, ranging from 1 to 31. |
hours | Optional. An integer representing the hour, between 0 and 23. |
minutes | Optional. An integer representing minutes, ranging from 0 to 59. |
seconds | Optional. An integer representing seconds, ranging from 0 to 59. |
ms | Optional. 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>
Output:
1123459200000
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's been: " + y + " years from 1970/01/01") document.write(" to 2005/07/08!") </script>
Output:
It's been: 35.62465753424657 years from 1970/01/01 to 2005/07/08!
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!