Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MomentJS getting JavaScript Date in UTC

I am not able to get the JavaScript Date string for MongoDB record via the following. It keeps using my local time.

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

Output:

Tue Nov 11 2014 14:42:51 GMT-0500 (EST) 

I need it to be in UTC, so I can stick this timestamp in Mongo so type would be Date.

How can I do that?

like image 942
Cmag Avatar asked Nov 11 '14 19:11

Cmag


People also ask

Is JavaScript date in UTC?

The Date. UTC() method in JavaScript is used to return the number of milliseconds in a Date object since January 1, 1970, 00:00:00, universal time. The UTC() method differs from the Date constructor in two ways: Date.

How do you convert a JavaScript date to UTC?

To convert a JavaScript date object to a UTC string, you can use the toUTCString() method of the Date object. The toUTCString() method converts a date to a string, using the universal time zone. Alternatively, you could also use the Date. UTC() method to create a new Date object directly in UTC time zone.

Does moment return UTC?

utc() will be in UTC mode, and any moment created with moment() will not.

Is new date () getTime () UTC?

Use the getTime() method to get a UTC timestamp, e.g. new Date(). getTime() . The method returns the number of milliseconds since the Unix Epoch and always uses UTC for time representation. Calling the method from any time zone returns the same UTC timestamp.


2 Answers

A timestamp is a point in time. Typically this can be represented by a number of milliseconds past an epoc (the Unix Epoc of Jan 1 1970 12AM UTC). The format of that point in time depends on the time zone. While it is the same point in time, the "hours value" is not the same among time zones and one must take into account the offset from the UTC.

Here's some code to illustrate. A point is time is captured in three different ways.

var moment = require( 'moment' );  var localDate = new Date(); var localMoment = moment(); var utcMoment = moment.utc(); var utcDate = new Date( utcMoment.format() );  //These are all the same console.log( 'localData unix = ' + localDate.valueOf() ); console.log( 'localMoment unix = ' + localMoment.valueOf() ); console.log( 'utcMoment unix = ' + utcMoment.valueOf() );  //These formats are different console.log( 'localDate = ' + localDate ); console.log( 'localMoment string = ' + localMoment.format() ); console.log( 'utcMoment string = ' + utcMoment.format() ); console.log( 'utcDate  = ' + utcDate );  //One to show conversion console.log( 'localDate as UTC format = ' + moment.utc( localDate ).format() ); console.log( 'localDate as UTC unix = ' + moment.utc( localDate ).valueOf() ); 

Which outputs this:

localData unix = 1415806206570 localMoment unix = 1415806206570 utcMoment unix = 1415806206570 localDate = Wed Nov 12 2014 10:30:06 GMT-0500 (EST) localMoment string = 2014-11-12T10:30:06-05:00 utcMoment string = 2014-11-12T15:30:06+00:00 utcDate  = Wed Nov 12 2014 10:30:06 GMT-0500 (EST) localDate as UTC format = 2014-11-12T15:30:06+00:00 localDate as UTC unix = 1415806206570 

In terms of milliseconds, each are the same. It is the exact same point in time (though in some runs, the later millisecond is one higher).

As far as format, each can be represented in a particular timezone. And the formatting of that timezone'd string looks different, for the exact same point in time!

Are you going to compare these time values? Just convert to milliseconds. One value of milliseconds is always less than, equal to or greater than another millisecond value.

Do you want to compare specific 'hour' or 'day' values and worried they "came from" different timezones? Convert to UTC first using moment.utc( existingDate ), and then do operations. Examples of those conversions, when coming out of the DB, are the last console.log calls in the example.

like image 149
clay Avatar answered Sep 20 '22 18:09

clay


Calling toDate will create a copy (the documentation is down-right wrong about it not being a copy), of the underlying JS Date object. JS Date object is stored in UTC and will always print to eastern time. Without getting into whether .utc() modifies the underlying object that moment wraps use the code below.

You don't need moment for this.

new Date().getTime() 

This works, because JS Date at its core is in UTC from the Unix Epoch. It's extraordinarily confusing and I believe a big flaw in the interface to mix local and UTC times like this with no descriptions in the methods.

like image 27
Timothy Gonzalez Avatar answered Sep 18 '22 18:09

Timothy Gonzalez