4

How do I convert a string like this back to a date object?

"Thu Aug 18 2011 15:13:55 GMT-0400 (Eastern Daylight Time)"

Is there a more native way to store dates in javascript?

3
  • 5
    sigh Man, no one puts out any effort to just search for answers. This type of question has been answered over and over here and many other places on the web. Commented Aug 18, 2011 at 19:24
  • 2
    geez. I just checked and noticed that everything has been answered at least once on the web, so let's close this board down because it adds nothing to the community. Even better, lets keep it going and just tell people to RTFM and just refer them to google. Commented Aug 19, 2011 at 22:20
  • Effort is appreciated here. It will get better answers. Strange, but true. :) Commented Aug 20, 2011 at 18:00

5 Answers 5

13

I've tested this in IE7, IE8, IE9, chrome, and firefox 6:

new Date('Thu Aug 18 2011 15:13:55 GMT-0400 (Eastern Daylight Time)');

and it works.

Sign up to request clarification or add additional context in comments.

Comments

2

http://www.w3schools.com/jsref/jsref_obj_date.asp provides some insight, just package it up and send it through and youll find all sorts of conveniance provided.

var d = new Date(year, month, day, hours, minutes, seconds, milliseconds);

1 Comment

Is there a format, that I can store as string and then convert back to js object without all the packaging & unpackaging?
0

If your dates will always be in a standard format (for sure), you could split into an array based on the space character and then create a date object from the items in the array.

Maybe not best approach but if your addresses are standardized, it might not be too bad, and probably pretty fast to implement/execute. :)

Comments

0

Date.parse(your date string) returns the number of milliseconds since January 1, 1970, 00:00:00 UTC. Store this number. When you want to display a date, use new Date(theNumber). Example:

var milliseconds = Date.parse("Thu Aug 18 2011 15:13:55 GMT-0400 (Eastern Daylight Time)");
// milliseconds == 1313694835000


alert(new Date(milliseconds));
// alerts  Thu Aug 18 2011 15:13:55 GMT-0400 (Eastern Daylight Time)

Comments

0

The Date object is quite accommodating so you can just use the string directly in a new object.

http://www.w3schools.com/js/js_obj_date.asp

new Date("Thu Aug 18 2011 15:13:55 GMT-0400 (Eastern Daylight Time)")

I say this a lot but when it comes to things like this, it's always awesome to experiment in the browser console and really get a feel for what the objects are capable of doing.. happy coding!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.