views:

75

answers:

4

I'm retrieving data from a JSON feed using jQuery and as part of the feed I'm getting 'datetime' attributes like "2009-07-01 07:30:09". I want to put this information into a javascript Date object for easy use but I don't believe the Date object would recognize this kind of format if I simply plugged it into the constructor. Is there a function or maybe a clever trick I can use to quickly break down this format into something the Date object can recognize and use?

+3  A: 

See this and this.

input = "2009-07-01 07:30:09";
var res =  input.match(/([\d\-]+) (\d+):(\d+):(\d+)/);
date = new Date(Date.parse(res[1])); 
date.setHours(res[2]);
date.setMinutes(res[3]);
date.setSeconds(res[4]);
console.log(date);

Edit: My original answer was

t = new Date(Date.parse("2009-07-01 07:30:09"));

which did not throw any error in chrome but all the same incorrectly parsed the date. This threw me off. Date.parse indeed appears to be quite flaky and parsing the complete date and time with it is probably not very reliable.

Edit2: DateJS appears to be a good solution for when some serious parsing of text to date is needed but at 25 kb it is a bit heavy for casual use.

anshul
@anshul> Does that work for you?
bdl
Just tested in firefox, I get "Invalid Date".
Alex Bagnolini
Nope. Tried doing...$('#date').html(t.getMonth() + '/' + t.getDay()); and it prints...NaN/NaN
Mathias Schnell
Yep... Date.parse doesn't work on the whole string in firefox and gives back now in the latest linux chrome beta, which is what I initially tested it out on. Thanks for pointing it out.
anshul
A: 

This may be a bit cumbersome, but the JavaScript Date object will take an argument list of YYYY,MM,DD,HH,MM,SS. Parse out the date value and pass it to a Date constructor, e.g.

var splitDT= '2009-07-01 07:30:09'.split(' '); // ['2009-07-01','07:30:09']
var d= splitDT[0].split('-');
var t= splitDT[1].split(':');
alert( (new Date(d[0],d[1],d[2],t[0],t[1],t[2])) );       

Bah. Had to use the array index values instead. Yeah, that's a mess. But it works.

bdl
A: 

var str="2009-07-01 07:30:09";

It depends on the time zone, and if the date string has subtracted 1 for the month.

If it is GMT time, and the 07 means July and not August:

var str="2009-07-01 07:30:09";

var d=new Date(), time;
str=str.split(/\D0?/);
str[1]-=1;
time=str.splice(3);
d.setUTCFullYear.apply(d,str);
d.setUTCHours.apply(d,time)
alert(d)

/* returned value: (Date) Wed Jul 01 2009 03:30:09 GMT-0400 (Eastern Daylight Time) or local equivilent */

kennebec
+3  A: 

The "date" attribute you are retrieving from that webservice is not a real Date, as it is not a recognized date format.

The easiest way to handle it as a Date object would be to replace the empty space with a "T":

var receivedDate = "2009-07-01 07:30:09";
var serializedDate = new Date(receivedDate.replace(" ", "T"));
alert(serializedDate);

This is not the most correct, as it is not handling timezones, but in most cases will work.

Alex Bagnolini
I tried this but for some reason it will not work. The space stays a space.
Mathias Schnell
Nevermind, it works. I was under the assumption that replace() affected the string it's called upon, not that it returns the result of the replacement. Not sure what language can/does do that, but I know there's some and sometimes I mix things up from language to language.
Mathias Schnell