1

i have a current time webservice that returns a JSON object like so

{
  "tz": "America\/Chicago", 
  "hour": 15, 
  "datetime": "Mon, 01 Apr 2013 15:46:58 -0500", 
  "second": 58, 
  "error": false, 
  "minute": 46
}

Is there an easy whay to convert the long-form datetime string

"datetime": "Mon, 01 Apr 2013 15:46:58 -0500" 

into a javascript Date object? (apart from using regex to parse the string)

Kijewski
  • 25,517
  • 12
  • 101
  • 143
Jeele Yah
  • 427
  • 7
  • 12
  • Maybe this helps: http://stackoverflow.com/q/1647550/218196. – Felix Kling Apr 02 '13 at 00:54
  • 1
    The format is defined in [RFC 3339](http://www.ietf.org/rfc/rfc3339.txt). It is very popular many in aged internet protocols. The only nice thing about the format is, that you do not need to use regex. The number of columns is fixed ("01"). – Kijewski Apr 02 '13 at 01:14

5 Answers5

2
var dt= "Mon, 01 Apr 2013 15:46:58 -050";
var date = new Date(dt);
alert(date.getDay());
PSL
  • 123,204
  • 21
  • 253
  • 243
  • It's probably not a good idea to rely on coincidence of an implementation dependent detail when standards compliant methods are available for marginally more effort. – RobG Apr 02 '13 at 02:16
  • It is _not_ implementation dependent. [The spec says](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date) that the Date ctor may receive an RFC2822 compliant dateString. – Kijewski Apr 02 '13 at 17:08
1
var dat = {
  "tz": "America\/Chicago", 
  "hour": 15, 
  "datetime": "Mon, 01 Apr 2013 15:46:58 -0500", 
  "second": 58, 
  "error": false, 
  "minute": 46
};

var dateObj = new Date(dat.datetime);

Mozilla Developer Network might show you some more helpful information. The Date constructor will parse the string for you.

ctlevi
  • 1,387
  • 1
  • 8
  • 7
  • -1: sorry, your answer is good save for the w3schools link. You ought to use [MDN](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date) links (or es5.github.com). See http://w3fools.com/ – Kijewski Apr 02 '13 at 01:02
  • That's really informative, thanks. It was the first link I saw in google search and the info on Date objects seemed good but I didn't know all that about W3schools. – ctlevi Apr 02 '13 at 01:06
0

This may be downvoted, but you may want to consider using momentjs for date/time manipulation. Not a bad library to use.

var myDate = moment(myObj.datetime);

Now myDate is a JavaScript Date object.

David Hoerster
  • 28,421
  • 8
  • 67
  • 102
0

The only safe way to parse a date string is to do it yourself. ES5 defines a standard string for Date.parse that is based on ISO8601 but it is not supported by all browsers in use, and your string isn't consistent with that format anyway.

Other string values "work" for a limited set of browsers, but that isn't a reliable strategy for a web application.

Parsing date strings is fairly simple: split up the bits, create a date object from the parts and apply an offset if required. So if your string is Mon, 01 Apr 2013 15:46:58 -0500 you can use a function like:

function parseDateString(s) {
  var months = {jan:0,feb:1,mar:2,apr:3,may:4,jun:5,
                jul:6,aug:7,sep:8,oct:9,nov:10,dec:11};
  s = s.split(/[\s:]/);

  var d = new Date(s[3], months[s[2].toLowerCase()], s[1], s[4], s[5], s[6]);
  var sign = s[7]<0? 1 : -1;
  var l = s[7].length;

  // offsetMinutes is minutes to add to time to get UTC
  var offsetMinutes = sign * s[7].substring(l-2,l) + sign * s[7].substring(l-4,l-2) * 60;

  // Add offset and subtract offset of current timezone
  d.setMinutes(d.getMinutes() + offsetMinutes - d.getTimezoneOffset());

  return d;
}

var s = 'Mon, 01 Apr 2013 15:46:58 -0500'
alert(s + '\n' + parseDateString(s));  // Mon, 01 Apr 2013 15:46:58 -0500
                                       // Tue Apr 02 2013 06:46:58 GMT+1000
RobG
  • 142,382
  • 31
  • 172
  • 209
0

/* You could rewrite the datestrings when you need them to a more common format- getting the month and timezone is most of the work: */

function rewriteDate(str){
    var months= ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 
    'jul', 'aug', 'sep', 'oct', 'nov', 'dec'],
    dA= str.toLowerCase().split(' '),
    m= months.indexOf(dA[2].substring(0, 3));

    ++m;
    if(m<10) m= '0'+m;
    dA[2]= m;

    var dmy= dA.slice(1, 4).reverse().join('-');

    var t= 'T'+dA[4], L= dA[5].length-2,
    z= dA[5].substring(0, L)+':'+dA[5].substring(L);
    return dmy+t+z;
}

var jsn={
    "datetime":"Mon, 01 Apr 2013 15:46:58 -0500"
};
jsn["datetime"]= rewriteDate(jsn.datetime);
//returns:  (string) "2013-04-01T15:46:58-05:00"


alert(new Date(jsn.datetime).toUTCString());

//  returns: (Date) Mon, 01 Apr 2013 20:46:58 GMT
kennebec
  • 102,654
  • 32
  • 106
  • 127