How do you get a timestamp in JavaScript?

前端 未结 30 3126
情深已故
情深已故 2020-11-21 15:19

How can I get a timestamp in JavaScript?

Something similar to Unix timestamp, that is, a single number that represents the current time and date. Either as a number

30条回答
  •  鱼传尺愫
    2020-11-21 15:50

    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.

提交回复
热议问题