convert iso date to milliseconds in javascript
Asked Answered
R

10

146

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.

Ruckman answered 10/2, 2012 at 14:20 Comment(5)
What do you mean by "milliseconds" exactly? Milliseconds relative to which point in time? Do you mean a UNIX timestamp?Weapon
just like in yahoo api, date is shown by milliseconds 1328796537, but in fb api, date is shown in iso 2012-02-10T13:18:45+0000Ruckman
possible duplicate of Help parsing ISO 8601 date in JavascriptEmigration
@Emigration related, but not exact. I want to get the difference between the two times.Ruckman
So use the link to translate them to dates first and do date1.getTime() - date2.getTime() - it is all you needEmigration
C
256

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.

Colettacolette answered 10/2, 2012 at 14:33 Comment(7)
Yes, Date will not parse on all browsers. See my answer on related question.Awful
And see my comment on the answer under yours ;)Emigration
This does not work in Firefox var myDate = new Date("2012-02-10T13:19:11+0000"); Emigration
@Emigration why you don't use this approach? I know this is cumbersome but anyway ))Colettacolette
uh... that's that capital "T" doing in there? That's why it doesn't work @mplungjan.Cruise
The T is an ISO date given by some systems. I remove it in another answer here at SOEmigration
this is a good solution. But offset should be multiplied by 60. var TIMEZONE_OFFSET = (new Date()).getTimezoneOffset()*60*1000;Astrodynamics
S
40

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");
Society answered 16/9, 2015 at 0:45 Comment(5)
You have a little typo there, '+'Grus
@stuart-siegler That's actually no typo, the '+' returns the Date in milliseconds.Exhibitionism
@Exhibitionism "+new" returns the date in miliseconds?Grus
@stuart-siegler Not specifically "+new" but the "+" does the trick and is a shorthand so-to-say. For example +new Date() returns 1447857230137 which is the time in milliseconds.Exhibitionism
It is much clearer to explicitly construct a JavaScript Number object: const dateMillis = Number(new Date('2017-09-26T20:24:18.801Z')), but new Date('2017-09-26T20:24:18.801Z').getTime() or new Date('2017-09-26T20:24:18.801Z').valueOf() is even more readable.Crabtree
E
7

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.

Elspeth answered 26/4, 2017 at 12:6 Comment(1)
The built–in parser is unreliable after ECMAScript 2011 also. :-)Marek
Y
5

Yes, you can do this in a single line

let ms = Date.parse('2019-05-15 07:11:10.673Z');
console.log(ms);//1557904270673
Yugoslav answered 15/5, 2019 at 9:32 Comment(1)
What is the opposite of this function!?Scandalize
J
3

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);
Joselyn answered 14/6, 2017 at 7:17 Comment(0)
T
3
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

Tabular answered 9/8, 2019 at 9:57 Comment(0)
A
2

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

Amity answered 16/7, 2018 at 12:10 Comment(0)
L
2

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

This worked for me!

Leeway answered 13/2, 2020 at 18:40 Comment(1)
how is this different from the top answer?Birddog
C
0

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);

Conflagration answered 3/7, 2020 at 10:31 Comment(0)
K
0

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
Kacikacie answered 29/9, 2020 at 2:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.