您如何获得JavaScript时间戳?

JavaScript

老丝米亚

2020-03-09

如何获取JavaScript时间戳?

Unix时间戳类似,即代表当前时间和日期的单个数字。可以是数字或字符串。

第152篇《您如何获得JavaScript时间戳?》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

18个回答
猪猪JinJin西里 2020.03.09

more simpler way:

var timeStamp=event.timestamp || new Date().getTime();
猿GO 2020.03.09

For lodash and underscore users, use _.now.

var timestamp = _.now(); // in milliseconds
小卤蛋Davaid 2020.03.09

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();
A小卤蛋Pro 2020.03.09

If it is for logging purposes, you can use ISOString

new Date().toISOString()

"2019-05-18T20:02:36.694Z"

米亚伽罗L 2020.03.09

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.

木心 2020.03.09

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();
小卤蛋小卤蛋小哥 2020.03.09

Any browsers not supported Date.now, you can use this for get current date time:

currentTime = Date.now() || +new Date()
江山如画 2020.03.09

// 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>

卡卡西十三 2020.03.09

You can only use

    var timestamp = new Date().getTime();
    console.log(timestamp);

to get the current timestamp. No need to do anything extra.

GilJinJin 2020.03.09

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

enter image description here

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.

小小阿飞 2020.03.09

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.

猴子Davaid老丝 2020.03.09

console.log(new Date().valueOf()); // returns the number of milliseconds since the epoch

Itachi伽罗 2020.03.09

In addition to the other options, if you want a dateformat ISO, you get can get it directly

console.log(new Date().toISOString());

Sam猪猪 2020.03.09

jQuery provides its own method to get the timestamp:

var timestamp = $.now();

(besides it just implements (new Date).getTime() expression)

REF: http://api.jquery.com/jQuery.now/

null 2020.03.09
var time = Date.now || function() {
  return +new Date;
};

time();
阿飞蛋蛋 2020.03.09
var timestamp = Number(new Date()); // current time as number
文韬武略辛弃疾 2020.03.09

JavaScript自时期起以毫秒为单位运行,而大多数其他语言以秒为单位运行。您可以以毫秒为单位工作,但是只要您传递一个值来表示PHP,PHP本机功能就可能会失败。因此,请确保我始终使用秒,而不是毫秒。

这将为您提供Unix时间戳(以秒为单位):

var unix = Math.round(+new Date()/1000);

这将为您提供自纪元以来的毫秒数(不是Unix时间戳):

var milliseconds = new Date().getTime();
伽罗理查德 2020.03.09

我喜欢这个,因为它很小:

+new Date

我也喜欢这样,因为它很短,并且与现代浏览器兼容,并且有500多人投票认为它更好:

Date.now()

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android