views:

1651

answers:

6

Hi,

I'm trying to get JavaScript to parse a date and time format for me, with the eventual aim of telling me the days passed since that date and the time right now (locally).

Unfortunately, the date format I have to work with (it's from a JSON response which I don't have control over) is returning it in 2008-10-01 06:21:43 type format.

var thedate = "2008-10-01 06:21:43";
var inmillisecs = new Date(thedate);

This just returns an error from JavaScript telling me the date is invalid.

How do I get around this issue?

A: 

The expected format is the American format: m/d/yyyy hh:mm:ss

var date1 = new Date("2008-10-01 06:21:43"); //fails
var date2 = new Date("10/1/2008 06:21:43"); //works correctly
Kip
thanks for clarifying this Kip - unfortunately, as stated, I don't have control over the format since it's coming from an API via JSON.I guess the only option I have is to regex replace everything, woopee.
jakeisonline
@Jake: this answer might help you out: http://stackoverflow.com/questions/1274743/how-can-i-determine-a-timezone-by-the-utc-offset/1274752#1274752
Kip
Sadly, no, the spec isn't even that specific. All it basically says is that new Date(string) should be able to parse whatever someDate.toString() spits out, which is implementation-dependent. http://www.ecma-international.org/publications/standards/Ecma-262.htm
T.J. Crowder
@Jake: Yup, looks like that's your best option, and then use the long form of the Date constructor that takes the parts individually. Still, the regex isn't that bad...
T.J. Crowder
The spec, such as it is, *does* say that the RFC822-style date format ‘Mon, 25 Dec 1995 13:30:00 GMT’ is supported. This is at least better than the US-style format with no timezone, though it is still pretty woeful. This sort of insanity is why it's generally best to deal with timestamps as integers (eg. in Unix-style seconds-since-epoch) whenever possible.
bobince
A: 

The correct syntax should be:

    var thedate = "Oct 1, 2008 06:21:43";
    var inmillisecs = new Date(thedate);

You have to take some steps to transform the String you're receiving into the format I showed. Using the american format also works

   var thedate = "10/1/2008 06:21:42";
   var inmillisecs = new Date(thedate);
Guilherme
A: 

it must be a string that is recognizable by the parse() function.

http://www.devguru.com/technologies/javascript/10585.asp look at the dateString param

contagious
+1  A: 

That's an ISO 9601 date -- they're a nice standard to work with. Try just munging it using regular expressions:

(\d{4})-(\d{2})-(\d{2})[ tT](.*)

to

\2/\3/\1 \4
Thom Smith
A: 

This should do it

function dateFromUTC( dateAsString, ymdDelimiter )
{
  var pattern = new RegExp( "(\\d{4})" + ymdDelimiter + "(\\d{2})" + ymdDelimiter + "(\\d{2}) (\\d{2}):(\\d{2}):(\\d{2})" );
  var parts = dateAsString.match( pattern );

  return new Date( Date.UTC(
      parseInt( parts[1] )
    , parseInt( parts[2], 10 ) - 1
    , parseInt( parts[3], 10 )
    , parseInt( parts[4], 10 )
    , parseInt( parts[5], 10 )
    , parseInt( parts[6], 10 )
    , 0
  ));
}

alert( dateFromUTC( "2008-10-01 06:21:43", '-' ) );
Peter Bailey
+2  A: 

There's this nice looking library called DateJS. I have no experience with it, but you might find it useful. I think you'd be particularly interested in parse() and/or parseExact().

I originally heard about it from this SO post.

Cheers.

EDIT: I just noticed your mention of time and I'm not sure DateJS handles times so I'm going to look into that real quick, or else you can just ignore this post :)

theIV