Home > Web Front-end > JS Tutorial > JavaScript method parse() based on the date and time according to the number of milliseconds between 1970/1/1 midnight (GMT time)

JavaScript method parse() based on the date and time according to the number of milliseconds between 1970/1/1 midnight (GMT time)

黄舟
Release: 2017-11-07 10:03:43
Original
2317 people have browsed it

Definition and Usage

The parse() method parses a date and time string and returns the number of milliseconds from midnight on January 1, 1970 to the date and time.

Syntax

Date.parse(datestring)
Copy after login
ParametersDescription
datestring Required. A string representing the date and time.

Return value

The number of milliseconds between the specified date and time as of midnight (GMT time) on January 1, 1970.

Description

This method is a static method of the Date object. This method is generally called in the form of Date.parse() instead of dateobject.parse().

Tips and Notes:

Note: Date.parse() is a static method of the Date object.

Example

Example 1

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

<script type="text/javascript">

var d = Date.parse("Jul 8, 2005")
document.write(d)

</script>
Copy after login

Output:

1120752000000
Copy after login

Example 2

Now, we will convert the output of the above example into adults:

<script type="text/javascript">

var minutes = 1000 * 60
var hours = minutes * 60
var days = hours * 24
var years = days * 365
var t = Date.parse("Jul 8, 2005")
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.538812785388124 years from 1970/01/01 to 2005/07/08!
Copy after login

The following are the details of the parameters:

datestring : 一个字符串,表示日期
Copy after login

Return value:

The number of milliseconds since midnight on January 1, 1970.
Example:

<html>
<head>
<title>JavaScript parse Method</title>
</head>
<body>
<script type="text/javascript">
  var msecs = Date.parse("Jul 8, 2008");
  document.write( "Number of milliseconds from 1970: " + msecs ); 
</script>
</body>
</html>
Copy after login

This will produce the following results:

Number of milliseconds from 1970: 1215455400000
Copy after login

The above is the detailed content of JavaScript method parse() based on the date and time according to the number of milliseconds between 1970/1/1 midnight (GMT time). 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