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)

2
  • Maybe this helps: stackoverflow.com/q/1647550/218196. Commented Apr 2, 2013 at 0:54
  • 1
    The format is defined in RFC 3339. 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"). Commented Apr 2, 2013 at 1:14

5 Answers 5

2
var dt= "Mon, 01 Apr 2013 15:46:58 -050";
var date = new Date(dt);
alert(date.getDay());
Sign up to request clarification or add additional context in comments.

2 Comments

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.
It is not implementation dependent. The spec says that the Date ctor may receive an RFC2822 compliant dateString.
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.

2 Comments

-1: sorry, your answer is good save for the w3schools link. You ought to use MDN links (or es5.github.com). See w3fools.com
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.
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

Comments

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.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.