Home > Web Front-end > JS Tutorial > The JavaScript method getTime() returns the number of milliseconds since January 1, 1970.

The JavaScript method getTime() returns the number of milliseconds since January 1, 1970.

黄舟
Release: 2017-11-06 13:08:12
Original
7318 people have browsed it

Definition and Usage

getTime() method returns the number of milliseconds since January 1, 1970.

Syntax

dateObject.getTime()
Copy after login

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>
Copy after login

Output:

1509944766448 milliseconds since 1970/01/01
Copy after login

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&#39;s been: " + y + " years since 1970/01/01!")

</script>
Copy after login

Output:

It&#39;s been: 47.88003445104008 years since 1970/01/01!
Copy after login

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
Copy after login

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>
Copy after login

This will produce the following results:

getTime() : 819913520000
Copy after login

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!

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