Getting current unixtimestamp using Moment.js

I want to get Unix TimeStamp using Moment.js. I can find many functions that convert a timestamp to a date in moment.js. I know that I can easily get the Unix timestamp using the following JavaScript function: Math.floor(new Date().getTime()/1000) .

But I want to use Moment.js to get the same result. Is there a direct function in moment.js to get the current timestamp?

+108
unix-timestamp momentjs
Aug 26 '14 at 6:11
source share
4 answers

To find the Unix timestamp in seconds:

 moment().unix() 

Documentation is your friend. :)

+203
Aug 26 '14 at 15:18
source share
— -

For those who find this page looking for a Unix millisecond timestamp, the documentation reads:

 moment().valueOf() 

or

 +moment(); 

you can also get it with moment().format('x') (or .format('X') [uppercase X] for unix seconds with decimal milliseconds), but this will give you a string. What moment .js will not actually be parsed afterwards unless you first convert / cast it back to a number.

+114
May 01 '15 at 8:27
source share

for UNIX timestamps in milliseconds

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

for UNIX timestamp in seconds moment().format('X') // capital X

+14
May 16 '16 at 5:52
source share

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 
+2
Mar 06 '19 at 21:01
source share



All Articles