Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting current unixtimestamp using Moment.js

People also ask

How can I get current time in moment?

To get the current date and time, just call javascript moment() with no parameters like so: var now = moment();

How do I get the current timestamp moment in Unix?

moment#valueOf simply outputs the number of milliseconds since the Unix Epoch, just like Date#valueOf . To get a Unix timestamp (the number of seconds since the epoch) from a Moment , use moment#unix .

What does moment now () return?

var now = moment(); This is essentially the same as calling moment(new Date()) . Note: From version 2.14.0, moment([]) and moment({}) also return now. They used to default to start-of-today before 2.14.0, but that was arbitrary so it was changed.

What is moment UNIX ()?

The moment(). unix() function is used to get the number of seconds since the Unix Epoch. Basically the Unix time is a system for describing a point in time. Syntax: moment().


To find the Unix Timestamp in seconds:

moment().unix()

The documentation is your friend. :)


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.

NOTE: This answer continues to get +1s, which is nice, but Moment has been depreciated, and alternatives like Luxon or date-fns are suggested. See: https://momentjs.com/docs/#/-project-status


for UNIX time-stamp in milliseconds

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

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


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