1

I have a string Mar 7 2012.

How do I convert this to a Date object, so I can use mydate.getDate(), mydate.getMonth(), mydate.getFullYear() etc?

oshirowanen
  • 15,297
  • 82
  • 198
  • 350
  • possible duplicate http://stackoverflow.com/questions/476105/how-can-i-convert-string-to-datetime-with-format-specification-in-javascript – jbabey Mar 07 '12 at 16:11
  • possible duplicate of [How to turn a string into a date in Javascript](http://stackoverflow.com/questions/2769901/how-to-turn-a-string-into-a-date-in-javascript) – James Montagne Mar 07 '12 at 16:12

2 Answers2

2

Well, Its pretty simple

var d =new Date("March 7 2012");
document.write(d.getMonth()); //use it
Starx
  • 77,474
  • 47
  • 185
  • 261
0

I'd use Date.parse in conjunction with Date#setTime. For example:

var d = new Date();
d.setTime(Date.parse("Mar 7 2012"));

You've pretty good reference docs in mozilla's dev. network: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date

  • 1
    and you'll notice that this is input is not compliant with the spec, so it's working by chance. – peller Mar 07 '12 at 16:56