1

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

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

Zoolander
  • 2,293
  • 5
  • 27
  • 37

5 Answers5

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>
Mike Christensen
  • 88,082
  • 50
  • 208
  • 326
  • Even though there's no code in the answer, +1 for a useful resource that I didn't know about! – Kiley Naro Sep 28 '11 at 16:03
  • 2
    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. – Peter Olson Sep 28 '11 at 16:07
  • 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`. – timrwood Sep 30 '11 at 22:00
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.

Peter Olson
  • 139,199
  • 49
  • 202
  • 242
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]; 
Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142
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();
João Louros
  • 2,752
  • 4
  • 23
  • 30
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');
timrwood
  • 10,611
  • 5
  • 35
  • 42