如何获取JavaScript时间戳?
与Unix时间戳类似,即代表当前时间和日期的单个数字。可以是数字或字符串。
如何获取JavaScript时间戳?
与Unix时间戳类似,即代表当前时间和日期的单个数字。可以是数字或字符串。
For lodash and underscore users, use _.now
.
var timestamp = _.now(); // in milliseconds
Moment.js can abstract away a lot of the pain in dealing with Javascript Dates.
See: http://momentjs.com/docs/#/displaying/unix-timestamp/
moment().unix();
If it is for logging purposes, you can use ISOString
new Date().toISOString()
"2019-05-18T20:02:36.694Z"
If want a basic way to generate a timestamp in Node.js this works well.
var time = process.hrtime();
var timestamp = Math.round( time[ 0 ] * 1e3 + time[ 1 ] / 1e6 );
Our team is using this to bust cache in a localhost environment. The output is /dist/css/global.css?v=245521377
where 245521377
is the timestamp generated by hrtime()
.
Hopefully this helps, the methods above can work as well but I found this to be the simplest approach for our needs in Node.js.
This one has a solution : which converts unixtime stamp to tim in js try this
var a = new Date(UNIX_timestamp*1000);
var hour = a.getUTCHours();
var min = a.getUTCMinutes();
var sec = a.getUTCSeconds();
Any browsers not supported Date.now, you can use this for get current date time:
currentTime = Date.now() || +new Date()
// The Current Unix Timestamp
// 1443534720 seconds since Jan 01 1970. (UTC)
// seconds
console.log(Math.floor(new Date().valueOf() / 1000)); // 1443534720
console.log(Math.floor(Date.now() / 1000)); // 1443534720
console.log(Math.floor(new Date().getTime() / 1000)); // 1443534720
// milliseconds
console.log(Math.floor(new Date().valueOf())); // 1443534720087
console.log(Math.floor(Date.now())); // 1443534720087
console.log(Math.floor(new Date().getTime())); // 1443534720087
// jQuery
// seconds
console.log(Math.floor($.now() / 1000)); // 1443534720
// milliseconds
console.log($.now()); // 1443534720087
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
You can only use
var timestamp = new Date().getTime();
console.log(timestamp);
to get the current timestamp. No need to do anything extra.
Today - 2018.06.27 I provide some time comparison for pure js solutions. This can be useful for people who wanna get/measure time in JS in light/efficient way (eg. for real-time applications like simulations, games etc.)
Tested on MacOs High Sierra 10.13.3 on Chrome 67.0.3396.99 (64-bit), Safari 11.0.3 (13604.5.6), Firefox 59.0.2 (64-bit). On below screenshot I show you results for fastest browser (Safari):
As I observe the Date.now()
was fastest method to get timestamp for all three browsers. Safari has 19.2M operations per second, Firefox 16.1M, Chrome 7.8M.
The new Date()*1
was slowest for Chrome (2.8M) and Firefox (2.6M). The Number(new Date())
was slowest for Safari (2.9M).
So the winner JS code is Date.now()
and fastest browser is Safari (2x faster that chrome! ).
You can perform test on your machine here: https://jsperf.com/timestamp-test-x.
The Date.getTime()
method can be used with a little tweak:
The value returned by the getTime method is the number of milliseconds since 1 January 1970 00:00:00 UTC.
Divide the result by 1000 to get the Unix timestamp, floor
if necessary:
(new Date).getTime() / 1000
The Date.valueOf()
method is functionally equivalent to Date.getTime()
, which makes it possible to use arithmetic operators on date object to achieve identical results. In my opinion, this approach affects readability.
console.log(new Date().valueOf()); // returns the number of milliseconds since the epoch
In addition to the other options, if you want a dateformat ISO, you get can get it directly
console.log(new Date().toISOString());
jQuery provides its own method to get the timestamp:
var timestamp = $.now();
(besides it just implements (new Date).getTime()
expression)
var time = Date.now || function() {
return +new Date;
};
time();
var timestamp = Number(new Date()); // current time as number
JavaScript自时期起以毫秒为单位运行,而大多数其他语言以秒为单位运行。您可以以毫秒为单位工作,但是只要您传递一个值来表示PHP,PHP本机功能就可能会失败。因此,请确保我始终使用秒,而不是毫秒。
这将为您提供Unix时间戳(以秒为单位):
var unix = Math.round(+new Date()/1000);
这将为您提供自纪元以来的毫秒数(不是Unix时间戳):
var milliseconds = new Date().getTime();
我喜欢这个,因为它很小:
+new Date
我也喜欢这样,因为它很短,并且与现代浏览器兼容,并且有500多人投票认为它更好:
Date.now()
more simpler way: