1

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.

SoluableNonagon
  • 11,541
  • 11
  • 53
  • 98
  • It's not a standard format, no. So first, decode the format. Looks to me like: `yyyymmdd-hh:mm:ss.sss` Second, parse the format yourself. Third, create a [new date](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) with that data. Voila. – Blazemonger Jun 22 '15 at 20:52
  • This format looks like a [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) but without dashes between years, months and days. You can put them manually and add "Z" symbol to the end and you will get in UTC+0 timezone. – arm.localhost Jun 22 '15 at 20:54
  • You could write a Regular Expression for this and parse it as suggested [here](http://stackoverflow.com/a/7712335/2376069). Saves a few lines but could be confusing to read. – Timo Mämecke Jun 22 '15 at 20:56

6 Answers6

2

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.

arm.localhost
  • 479
  • 4
  • 9
1

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
Cᴏʀʏ
  • 105,112
  • 20
  • 162
  • 194
1

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.

Community
  • 1
  • 1
Cymen
  • 14,079
  • 4
  • 52
  • 72
1

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);
Pablo
  • 1,073
  • 8
  • 9
0

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();
schirrmacher
  • 2,341
  • 2
  • 27
  • 29
0

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();`
Luke Benting
  • 168
  • 11
  • Thank captain, but it is not answer for his question :) – Jozef Dúc Jun 22 '15 at 21:00
  • It doesn't seem that OP is getting this date string from a source, they just have it hardcoded in their javascript, so providing a solution that is complex and converts a wrongly formatted date string into a correctly formatted date string might be overkill and confusing. – Luke Benting Jun 22 '15 at 21:04