How do you get a timestamp in JavaScript?

前端 未结 30 3075
情深已故
情深已故 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条回答
  •  -上瘾入骨i
    2020-11-21 15:46

    I highly recommend using moment.js. To get the number of milliseconds since UNIX epoch, do

    moment().valueOf()
    

    To get the number of seconds since UNIX epoch, do

    moment().unix()
    

    You can also convert times like so:

    moment('2015-07-12 14:59:23', 'YYYY-MM-DD HH:mm:ss').valueOf()
    

    I do that all the time. No pun intended.

    To use moment.js in the browser:

    
    
    

    For more details, including other ways of installing and using MomentJS, see their docs

提交回复
热议问题