Definition and Usage
getTime() method returns the number of milliseconds since January 1, 1970.
Syntax
dateObject.getTime()
Return Value
dateObject The number of milliseconds between the specified date and time since midnight on January 1, 1970 (GMT time).
Tips and Comments:
Comments: This method is always used in conjunction with a Date object.
Example
Example 1
In this example, we will get the number of milliseconds from 1970/01/01 to the present and output it:
<script type="text/javascript"> var d = new Date() document.write(d.getTime() + " milliseconds since 1970/01/01") </script>
Output:
1509944766448 milliseconds since 1970/01/01
Example 2
In the following example, we will calculate how many years have passed since 1970/01/01:
<script type="text/javascript"> var minutes = 1000*60 var hours = minutes*60 var days = hours*24 var years = days*365 var d = new Date() var t = d.getTime() var y = t/years document.write("It's been: " + y + " years since 1970/01/01!") </script>
Output:
It's been: 47.88003445104008 years since 1970/01/01!
Example & Description
// 定义一个当前时间的Date对象(2014-08-07) var date = new Date(); document.writeln( date.getTime() ); // 1407404680731 // 定义一个"1970-01-01 08:00:00 000"的Date对象 // 由于当前环境为北京GMT+8时区,所以与GMT有8个小时的差值 var date2 = new Date(1970, 0, 1, 8); document.writeln( date2.getTime() ); // 0 // 定义一个"1965-8-18"的Date对象 var date3 = new Date(1965, 7, 18); document.writeln( date3.getTime() ); // -138009600000
Example:
<html> <head> <title>JavaScript getTime Method</title> </head> <body> <script type="text/javascript"> var dt = new Date( "December 25, 1995 23:15:20" ); document.write("getTime() : " + dt.getTime() ); </script> </body> </html>
This will produce the following results:
getTime() : 819913520000
The above is the detailed content of The JavaScript method getTime() returns the number of milliseconds since January 1, 1970.. For more information, please follow other related articles on the PHP Chinese website!