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!

2

2 Answers 2

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

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

1 Comment

+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.
0

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

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.