Question

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)

Was it helpful?

Solution 3

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

OTHER TIPS

var dt= "Mon, 01 Apr 2013 15:46:58 -050";
var date = new Date(dt);
alert(date.getDay());
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.

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.

/* 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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top