convert iso date to milliseconds in javascript

JavascriptIsoIsodate

Javascript Problem Overview


Can I convert iso date to milliseconds? for example I want to convert this iso

2012-02-10T13:19:11+0000

to milliseconds.

Because I want to compare current date from the created date. And created date is an iso date.

Javascript Solutions


Solution 1 - Javascript

Try this

var date = new Date("11/21/1987 16:00:00"); // some mock date
var milliseconds = date.getTime(); 
// This will return you the number of milliseconds
// elapsed from January 1, 1970 
// if your date is less than that date, the value will be negative

console.log(milliseconds);

EDIT

You've provided an ISO date. It is also accepted by the constructor of the Date object

var myDate = new Date("2012-02-10T13:19:11+0000");
var result = myDate.getTime();
console.log(result);

Edit

The best I've found is to get rid of the offset manually.

var myDate = new Date("2012-02-10T13:19:11+0000");
var offset = myDate.getTimezoneOffset() * 60 * 1000;

var withOffset = myDate.getTime();
var withoutOffset = withOffset - offset;
console.log(withOffset);
console.log(withoutOffset);

Seems working. As far as problems with converting ISO string into the Date object you may refer to the links provided.

EDIT

Fixed the bug with incorrect conversion to milliseconds according to Prasad19sara's comment.

Solution 2 - Javascript

A shorthand of the previous solutions is

var myDate = +new Date("2012-02-10T13:19:11+0000");

It does an on the fly type conversion and directly outputs date in millisecond format.

Another way is also using parse method of Date util which only outputs EPOCH time in milliseconds.

var myDate = Date.parse("2012-02-10T13:19:11+0000");

Solution 3 - Javascript

Another option as of 2017 is to use Date.parse(). MDN's documentation points out, however, that it is unreliable prior to ES5.

var date = new Date(); // today's date and time in ISO format
var myDate = Date.parse(date);

See the fiddle for more details.

Solution 4 - Javascript

Yes, you can do this in a single line

let ms = Date.parse('2019-05-15 07:11:10.673Z');
console.log(ms);//1557904270673

Solution 5 - Javascript

Another possible solution is to compare current date with January 1, 1970, you can get January 1, 1970 by new Date(0);

var date = new Date(); 
var myDate= date - new Date(0);

Solution 6 - Javascript

Another solution could be to use Number object parser like this:

let result = Number(new Date("2012-02-10T13:19:11+0000"));
let resultWithGetTime = (new Date("2012-02-10T13:19:11+0000")).getTime();
console.log(result);
console.log(resultWithGetTime);

This converts to milliseconds just like getTime() on Date object

Solution 7 - Javascript

var date = new Date()
console.log(" Date in MS last three digit = "+  date.getMilliseconds())
console.log(" MS = "+ Date.now())

Using this we can get date in milliseconds

Solution 8 - Javascript

var date = new Date(date_string); var milliseconds = date.getTime();

This worked for me!

Solution 9 - Javascript

if wants to convert UTC date to milliseconds
syntax : Date.UTC(year, month, ?day, ?hours, ?min, ?sec, ?milisec);
e.g :
date_in_mili = Date.UTC(2020, 07, 03, 03, 40, 40, 40);
console.log('miliseconds', date_in_mili);

Solution 10 - Javascript

In case if anyone wants to grab only the Time from a ISO Date, following will be helpful. I was searching for that and I couldn't find a question for it. So in case some one sees will be helpful.

let isoDate = '2020-09-28T15:27:15+05:30';
let result = isoDate.match(/\d\d:\d\d/);
console.log(result[0]);

The output will be the only the time from isoDate which is,

15:27

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRobin Carlo CatacutanView Question on Stackoverflow
Solution 1 - JavascriptOybekView Answer on Stackoverflow
Solution 2 - JavascriptTahsin TurkozView Answer on Stackoverflow
Solution 3 - Javascriptsamurai_janeView Answer on Stackoverflow
Solution 4 - JavascriptMohid KaziView Answer on Stackoverflow
Solution 5 - JavascriptLingYan MengView Answer on Stackoverflow
Solution 6 - JavascriptBlack MambaView Answer on Stackoverflow
Solution 7 - JavascriptJibinNajeebView Answer on Stackoverflow
Solution 8 - JavascriptVisakh VijayanView Answer on Stackoverflow
Solution 9 - JavascriptSiddharthView Answer on Stackoverflow
Solution 10 - JavascriptNiroshan RatnayakeView Answer on Stackoverflow