Getting current unixtimestamp using Moment.js

前端 未结 4 1693
温柔的废话
温柔的废话 2021-01-30 05:57

I want to get the Unix TimeStamp using Moment.js. I can find many functions which convert timestamp to date in moment.js. I know that I can easily get the unix timestamp by usin

相关标签:
4条回答
  • 2021-01-30 06:31

    To find the Unix Timestamp in seconds:

    moment().unix()
    

    The documentation is your friend. :)

    0 讨论(0)
  • 2021-01-30 06:40

    For anyone who finds this page looking for unix timestamp w/ milliseconds, the documentation says

    moment().valueOf()
    

    or

    +moment();
    

    you can also get it through moment().format('x') (or .format('X') [capital X] for unix seconds with decimal milliseconds), but that will give you a string. Which moment.js won't actually parse back afterwards, unless you convert/cast it back to a number first.

    0 讨论(0)
  • 2021-01-30 06:41

    Try any of these

    valof = moment().valueOf();            // xxxxxxxxxxxxx
    getTime = moment().toDate().getTime(); // xxxxxxxxxxxxx
    unixTime =  moment().unix();           // xxxxxxxxxx
    formatTimex =  moment().format('x');   // xxxxxxxxxx
    unixFormatX = moment().format('X');    // xxxxxxxxxx
    
    0 讨论(0)
  • 2021-01-30 06:45

    for UNIX time-stamp in milliseconds

    moment().format('x') // lowerCase x

    for UNIX time-stamp in seconds moment().format('X') // capital X

    0 讨论(0)
提交回复
热议问题