SoluableNonagon
SoluableNonagon

Reputation: 11755

Unformatting a date in JS - convert to milliseconds

I have a date string I would like to convert to milliseconds. Though I don't know what format this is.

var time = "20150605-16:34:53.506";

I'm unsure how to get this time into milliseconds.

I tried

new Date(time); 

but it just gave me an error that it's not a valid time.

I'd like to achieve this without libraries if possible.

Upvotes: 1

Views: 933

Answers (6)

arm.localhost
arm.localhost

Reputation: 479

You can do something like this:

var time = "20150605-16:34:53.506";
var date = new Date(time.replace(/(\d{4})(\d{2})(\d{2})-(.*)/, '$1-$2-$3T$4Z'));
var ms = date.getTime();
console.log(ms);

This regular expression looks weird but it do it's work.

Upvotes: 2

Cymen
Cymen

Reputation: 14419

Here is the brute force method:

var time = "20150605-16:34:53.506";
var year = parseInt(time.substr(0, 4), 10);
var month = parseInt(time.substr(4, 2), 10) - 1;
var day = parseInt(time.substr(6, 2), 10);
var hour = parseInt(time.substr(9, 2), 10);
var minute = parseInt(time.substr(12, 2), 10);
var second = parseInt(time.substr(15, 2), 10);
var mille = parseInt(time.substr(18, 3), 10);
var date = new Date(year, month, day, hour, minute, second, mille);
console.log(date);
> Fri Jun 05 2015 16:34:53 GMT-0700 (PDT)

See MDN Date for more details. Note that month is odd -- it is zero-based unlike year and day (thus the -1 on the parsed month):

month

Integer value representing the month, beginning with 0 for January to 11 for December.

Upvotes: 1

Luke Benting
Luke Benting

Reputation: 168

The string you are trying to parse to a date is not in a format javascript recognises, here is a format it can, and how you would get the millisecond value:

var d = new Date("June 05, 2015 16:34:53:506");
var n = d.getMilliseconds();`

Upvotes: 0

Pablo
Pablo

Reputation: 1093

According to this the Date is some kind of ISO 8601 format (it just says that the standard separator is a 'T').

Date.parse() doesn't accept you format right away, you must do 2 changes: Change the dash '-' for a 'T' and separate the date

str = "20150605-16:34:53.506";
formattedStr = str.slice(0,4) + '-' + str.slice(5,7) + '-' + str.slice(8).replace('-', 'T');
Date.parse(formattedStr);

Upvotes: 1

schirrmacher
schirrmacher

Reputation: 2357

Change the string into a format that JavaScript recognizes (It accepts the RFC2822 / IETF date syntax). Then use:

var time = Date.parse(yourFormattedTimeString);

Then:

var milliseconds = time.getMilliseconds();

Upvotes: 0

Cᴏʀʏ
Cᴏʀʏ

Reputation: 107528

You can transform that string into a valid date string with a couple easy steps, the goal being to get from:

20150605-16:34:53.506

To:

2015-06-05T16:34:53.506

var time = '20150605-16:34:53.506';
time = time.replace('-', 'T');
time = time.slice(0, 4) + '-' + time.slice(4, 6) + '-' + time.slice(6);

var milliseconds = new Date(time).getTime();
console.log(milliseconds); // 1433540093506

Upvotes: 1

Related Questions