How do you get a timestamp in JavaScript?

前端 未结 30 3077
情深已故
情深已故 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:31

    The code Math.floor(new Date().getTime() / 1000) can be shortened to new Date / 1E3 | 0.

    Consider to skip direct getTime() invocation and use | 0 as a replacement for Math.floor() function. It's also good to remember 1E3 is a shorter equivalent for 1000 (uppercase E is preferred than lowercase to indicate 1E3 as a constant).

    As a result you get the following:

    var ts = new Date / 1E3 | 0;
    
    console.log(ts);

提交回复
热议问题