Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you convert Milliseconds into a Javascript UTC date?

Given I have the number 1446309338000, how do I create a JavaScript UTC date?

new Date(1446309338000) will equal a CST time (central standard) or local time.
new Date(Date.UTC(year, month, day, hour, minute, second)) haven't got this info yet.

Does JavaScript change the time if I do this?

new Date(1446309338000).ISOString();

Is it creating a new CST date and then converting it to UTC? I really just need the string. I am taking it from a database (RowKey from a Azure Table storage database).

like image 751
markthegrea Avatar asked Nov 03 '15 19:11

markthegrea


People also ask

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.

How do I get a UTC timestamp in JavaScript?

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.

Is UTC time in milliseconds?

There is no such things as "UTC milliseconds". A millisecond is an SI unit that is one thousandth of a second. ECMAScript Date instances hold a time value that is an offset in milliseconds from 1970-01-01T00:00:00Z (the ECMAScript epoch).

Does JavaScript Date use 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.


3 Answers

If you have the milliseconds that's already the UTC date. Which basically means the universal time. Now based on those millis you can convert the Date object into a String of your like:

new Date(1446309338000).toUTCString() // timezone free universal format
> "Sat, 31 Oct 2015 16:35:38 GMT"
new Date(1446309338000).toString() // browser local timezon string
> "Sat Oct 31 2015 09:35:38 GMT-0700 (PDT)"
new Date(1446309338000).toISOString() // ISO format of the UTC time
> "2015-10-31T16:35:38.000Z"

Now, if for some reason (I can't see a valid reason, but just for the heck of it) you're looking for having a different amount of milliseconds that represent a different date but that would print the same in the local browser timezone, you can do this calculation:

new Date(1446309338000 - new Date(1446309338000).getTimezoneOffset() * 60 * 1000))

Now toString from original Date and toUTCString of this new Date would read the same up to the Timezone information, because of course they're not the same date!

new Date(1446309338000).toString()
> "Sat Oct 31 2015 09:35:38 GMT-0700 (PDT)"
new Date(1446309338000 - new Date(1446309338000).getTimezoneOffset() * 60 * 1000).toUTCString()
> "Sat, 31 Oct 2015 09:35:38 GMT"
like image 190
Ulises Avatar answered Sep 24 '22 19:09

Ulises


It's actually as simple as homemade biscuits, If you have your date, say:

var date_in_milliseconds = 1504640419000;


You can then initialize a new date like this:

var human_readable_date = new Date(0); //Date(0) creates a date at the Epoch, so Wed Dec 31 1969

now, just add the milliseconds to the Epoch, and this will give us the desired date:

human_readable_date.setUTCMilliseconds(date_in_milliseconds);
like image 27
Big Sam Avatar answered Sep 23 '22 19:09

Big Sam


Well, if the date string is what you require, hope this helps:

new Date(1446309338000).toLocaleString('en-US', {timeZone: 'UTC'})

As far as toISOString() is concerned, it returns string representation using ISO-8601 standard (the format is: YYYY-MM-DDTHH:mm:ss.sssZ).
toLocaleString() is human readable format with same result.

like image 31
abksrv Avatar answered Sep 21 '22 19:09

abksrv