1

Let's say that I have date as a string, like: 02-12-2011

How can I parse it, and make it in format:

Friday, 02 December, 2011.

Thank you in advance!

cweiske
  • 30,033
  • 14
  • 133
  • 194
user198003
  • 11,029
  • 28
  • 94
  • 152

2 Answers2

2

Something like this should work:

var date = "02-12-2011".split('-');

var month = (date[0] * 1 ) - 1; // * 1 to convert to Number - 1 to subtract to 1 (months are from 0 - 11)
var day   = date[1];
var year  = data[2];

var d = new Date();
d.setMonth(month);
d.setDate(day);
d.setFullYear(year);

console.log(d.toDateString()); // will output Sat Feb 12 2011

You could also format the date differently by creating your own function that uses the getters getMonth(), getDate(), getFullYear(), getDay().

If you'd like a lighter weight solution. Otherwise the link that @diEcho mentions looks good.

Also, the W3School references, while not the best for style, are pretty decent for a general 'get to the facts' reference about the various JavaScript Objects.

Here's a link to the Date object: http://www.w3schools.com/jsref/jsref_obj_date.asp

  • +1 for `month = ... -1`. Stupid JavaScript Date object thinks months run from 0 to 11. Not days, not years, just months. I hate amateur-designed languages. – Ross Patterson Oct 31 '11 at 23:41
0

This blog article on JavaScript date formatting will help on the formatting part.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
xkeshav
  • 53,360
  • 44
  • 177
  • 245