How do you get a timestamp in JavaScript?

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

    JavaScript works with the number of milliseconds since the epoch whereas most other languages work with the seconds. You could work with milliseconds but as soon as you pass a value to say PHP, the PHP native functions will probably fail. So to be sure I always use the seconds, not milliseconds.

    This will give you a Unix timestamp (in seconds):

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

    This will give you the milliseconds since the epoch (not Unix timestamp):

    var milliseconds = new Date().getTime();
    

提交回复
热议问题