12

Possible Duplicate:
How can I convert string to datetime with format specification in JavaScript?

I have a json response which contains a hashmap like;

{"map":{"2012-10-10 03:47:00.0":23.400000000000002,"2012-10-10 03:52:00.0":23.3,"2012-10-10 03:57:00.0":23.3,"2012-10-10 04:02:00.0":23.3,"2012-10-10 04:07:00.0":23.200000000000003,"2012-10-10 04:13:00.0":23.1,"2012-10-10 04:18:00.0":23.1,"2012-10-10 04:23:00.0":23.0,"2012-10-10 04:28:00.0":23.0,"2012-10-10 04:33:00.0":23.0,"2012-10-10 04:38:00.0":22.900000000000002,"2012-10-10 04:43:00.0":22.8,"2012-10-10 04:48:00.0":22.8,"2012-10-10 04:53:00.0":22.700000000000003,"2012-10-10 04:58:00.0":22.6,"2012-10-10 05:03:00.0":22.6,"2012-10-10 05:08:00.0":22.5,"2012-10-10 05:13:00.0":22.5,"2012-10-10 05:18:00.0":22.5,"2012-10-10 05:23:00.0":22.400000000000002}}

I want to format datetime part of json like;

dd/mm/yyyy HH:mm:ss

Lets assume I put all pair elements like this;

var myArr = [["2012-10-10 03:47:00.0", 23.400000000000002], ["2012-10-10 03:52:00.0", 23.3], ....];

Then, I try to parse datetime part like below and I got Date {Invalid Date} on console;

new Date(myArr[0][0]);

How can I format this type of datetime.

Community
  • 1
  • 1
vtokmak
  • 1,496
  • 6
  • 35
  • 66
  • 1
    I do get a valid date out of `new Date(myArr[0][0])`. (And it should never throw an exception, in the first place.) – pimvdb Oct 10 '12 at 18:08
  • Actually, it is not an exception, when I use `console.log(new Date(myArr[0][0]));` its output `Date {Invalid Date}` – vtokmak Oct 10 '12 at 18:17

2 Answers2

24

Try the following:

new Date(Date.parse(myArr[0][0]));

EXAMPLE

Use the Date.parse method to parse the string into the number of milliseconds since January 1, 1970, 00:00:00 UTC. Take that number of milliseconds and call the Date method once again to turn that time into a date object.

EDIT:

Well this may be a little ugly for this case, but it seems Firefox is having an issue with the -s and the 00.0.

var myArr = [["2012-10-10 03:47:00.0", 23.400000000000002], ["2012-10-10 03:52:00.0", 23.3]];

var date = convertDateTime(myArr[0][0]);
console.log(date);

function convertDateTime(dateTime){
    dateTime = myArr[0][0].split(" ");

    var date = dateTime[0].split("-");
    var yyyy = date[0];
    var mm = date[1]-1;
    var dd = date[2];

    var time = dateTime[1].split(":");
    var h = time[0];
    var m = time[1];
    var s = parseInt(time[2]); //get rid of that 00.0;

    return new Date(yyyy,mm,dd,h,m,s);
}

EXAMPLE

Chase
  • 29,019
  • 1
  • 49
  • 48
  • On your example still have invalid date error – vtokmak Oct 10 '12 at 18:23
  • It gives error on firefox's console :/ Chrome renders fine. – vtokmak Oct 10 '12 at 18:25
  • `new Date` accepts a `Date.parse` string, as noted in the MDN article you linked to. Thus this answer would not solve the issue. – pimvdb Oct 10 '12 at 18:28
  • @vtokmak, the updated example should work for you and I would suggest creating a function that takes a string (`"2012-10-10 03:47:00.0"`) that way you can loop through your result set and call the function. – Chase Oct 10 '12 at 18:45
11
function dateFromString(str) {
  var m = str.match(/(\d+)-(\d+)-(\d+)\s+(\d+):(\d+):(\d+)\.(\d+)/);
  return new Date(+m[1], +m[2] - 1, +m[3], +m[4], +m[5], +m[6] * 100);
}

dateFromString(myArr[0][0]); // Sat Oct 10 2012 03:47:00 GMT-0500 (EST)
jimbo
  • 11,004
  • 6
  • 29
  • 46
  • Slight variation to support Facebook dates: str.match(/(\d+)-(\d+)-(\d+)[\sT]+(\d+):(\d+):(\d+)[.+](\d+)/) – Dunc Jul 16 '14 at 15:34
  • Why `m[6] * 100`? m[6] is just seconds, should be passed to the Date constructor without a multiplicator. – Stan Sep 16 '14 at 12:16