1

I have a string in the format MM/DD/YYYY.

Can someone tell me how to convert it to: January 1, 2011?

3
  • Do you want it in English only or in locale-specific language? Commented Sep 28, 2011 at 15:50
  • Also, possibly duplicate of stackoverflow.com/questions/1643320/… Commented Sep 28, 2011 at 15:51
  • -1 Your question doesn't show any research effort at all! What have you tried, and what are the complications? Commented Sep 28, 2011 at 16:03

5 Answers 5

4

If you haven't already, check out date.js -

http://www.datejs.com/

It's great for any date related functions, and has tons of examples on their site to do everything you can imagine.

Update: Ok some people doubt my mad coding skillz :)

<script src="http://www.datejs.com/build/date.js" type="text/javascript"></script>

<script language="Javascript">
  var d = Date.parse('03/08/1980'); 
  window.alert(d.toString('MMMM d, yyyy'));
</script>
Sign up to request clarification or add additional context in comments.

3 Comments

Even though there's no code in the answer, +1 for a useful resource that I didn't know about!
If you recommend a library to do this, you should at least show how to do what the person is asking for. I'll rescind my downvote if you edit to show how to do this with date.js.
DateJS is not that great of a library, it seriously messes with the Native Date prototype. It adds over 20 properties to the Date object, and over 90 methods to the Date.prototype, including overriding .toString.
2
var str = "01/01/2011",
date = new Date(str),
months = ["January", "February", "March", "April", "May", "June", 
          "July", "August", "September", "October", "November", "December"],
converted = months[date.getMonth()] + " " + 
            date.getDate() + ", " + date.getFullYear();

See it in action.

Comments

1

I hope this helps

var dat = new Date("01/01/2011");
var monthNames = [ "January", "February", "March", "April", "May", "June","July", "August", "September", "October", "November", "December" ];
var stringDate = monthNames[dat.getMonth()] + ", " + dat.getDate() + ", " + dat.getFullYear();

Comments

1
var str = "1/1/2011";

var monthNames = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ];    
var parts = str.split("/");
var formatted = monthNames[parseInt(parts[0], 10)-1] + " " + parts[1] + ", " + parts[2]; 

Comments

1

I wrote a date library similar to DateJS, only it's smaller, faster, and doesn't modify the Date.prototype. It handles parsing, manipulating, and formatting, including timeago.

I see that you only need english, but there is support for i18n in underscore.date.

https://github.com/timrwood/underscore.date

With underscore.date, this is how you would solve your problem.

_date('01/01/2011', 'MM/DD/YYYY').format('MMMM D, YYYY');

2 Comments

So MMMM returns the name of the month?
Correct, MMMM = full name of month, MMM = abbreviated name of the month. All the replacement keys are in the docs at github.com/timrwood/underscore.date

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.