Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

momentJS get UTC timestamp

Folks, I am unable to get the UTC timestamp using the momentjs. Hope someone can point me in the right direction

var start = Date.now(); var utc = moment.utc(start).toDate(); 

or

var utc = moment.utc().toDate(); Tue Nov 11 2014 13:45:13 GMT-0500 (EST) 

Returns the EST timezone I am in, not UTC. How do i get the Javascript Date in UTC?

If I do

var utc= moment.utc(); console.log(utc); 

output is

 { _useUTC: true,   _isUTC: true,   _l: undefined,   _i: undefined,   _f: undefined,   _d: Tue Nov 11 2014 13:43:21 GMT-0500 (EST) } 

Thanks

like image 460
Cmag Avatar asked Nov 11 '14 18:11

Cmag


People also ask

How do I convert UTC time to local time in MomentJs?

utc(). format('YYYY-MM-DD HH:mm:ss'); var localTime = moment. utc(date). toDate(); localTime = moment(localTime).

How do I get UTC datetime in moment?

utc(Date); By default, moment parses and displays in local time. If you want to parse or display a moment in UTC, you can use moment. utc() instead of moment() .

How do I get time from MomentJs?

You can directly call function momentInstance. valueOf(), it will return numeric value of time similar to date. getTime() in native java script.

Is MomentJs deprecated?

MomentJs recently announced that the library is now deprecated. This is a big deal for the javascript community who actively downloads moment almost 15 million times a week.


1 Answers

Simplest answer from the comments :

moment.utc().valueOf() 

Gives UTC timestamp

like image 91
Charles Foster Avatar answered Sep 23 '22 04:09

Charles Foster