Home >Web Front-end >JS Tutorial >JavaScript code to get the current timestamp_time and date

JavaScript code to get the current timestamp_time and date

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-05-16 18:21:531566browse

JavaScript to get the current timestamp:
The first method:

Copy code The code is as follows:

var timestamp = Date.parse(new Date());

Result: 1280977330000
Second method:
Copy code The code is as follows:

var timestamp = (new Date()).valueOf();

Result: 1280977330748

The above code will get the number of milliseconds since midnight on January 1, 1970. The difference between the two is that the millisecond bit of the first method is all zeros, that is, it is only the number of milliseconds accurate to seconds

As shown in the title, return the specific time corresponding to the unix timestamp:
Copy code The code is as follows:

var time = '1278927966';
//The key is to multiply by 1000, because the time is relative to the beginning of 1970, so multiplying by 1000 will go to the current time.
var real_time = new Date(time) * 1000;
document.write(real_time);

The code is very simple to complete the timestamp conversion.

Use new Date().getTime() method in javascript

IE8 and above versions can be used directly using the Date.now() method

//Versions below IE8
if (!Date.now) {
Date.now = function() { return new Date().getTime(); };
}

jQuery gets timestamp $.now()

var timestamp = $.now();

The following are additions from other netizens:

JavaScript to get the current timestamp:

First method:

var timestamp = Date.parse(new Date());

Result: 1280977330000
Second method:

var timestamp = (new Date()).valueOf();

Result: 1280977330748

The third method:

var timestamp=new Date().getTime();
Result: 1280977330748

The first method: the timestamp obtained is to change the milliseconds to 000 for display,
The second and third methods obtain the timestamp of the current millisecond.

Statement:
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