0

I have a date in the format MM/DD/YYYY. I need to then convert it into , using javascript. (More exact example: 05/25/2012 --> "May 25, 2012") I know how to convert the dates, but by question is:

Is there a more efficient way, whether using a javascript function or something else, to convert the MM/DD/YYYY to the format above instead of using a switch statement and concatenating strings?

Proxy404
  • 189
  • 1
  • 2
  • 8

3 Answers3

1

You may find the parseDate and formatDate functions in the Datepicker of jQuery UI appealing, e.g.

var date = $.datepicker.parseDate('mm/dd/yy', '05/25/2012');
$.datepicker.formatDate('MM d yy', date);

It all depends how you define 'efficient':

  • Code density? - This is efficient imho.
  • Speed and performance? - No clue.
  • Localization? - It is supported, e.g. $(selector).datepicker($.datepicker.regional['fr']);*
matsev
  • 32,104
  • 16
  • 121
  • 156
0

Strangely enough, I just posted a related question about half an hour ago. Here's what I'm using, but it also parses a time.

// Date m/d/Y Time h:m a
function parseDate(date,time) {

date = date.split("/");
time = time.split(" ");
hm = time[0].split(':'); 
if (parseInt(hm[0],10) == 12) {
    hm[0] = 0;
}
if (time[1] == 'pm') {
    hm[0] = parseInt(hm[0],10) + 12;
} else {
    hm[0] = parseInt(hm[0],10);
}
return new Date(
    parseInt(date[2],10), 
    parseInt(date[0],10)-1, 
    parseInt(date[1],10),
    hm[0],
    parseInt(hm[1],10)
);

}

So you could easily remove the time lines and have the following...

function parseDate(date) {

  date = date.split("/");

  return new Date(
    parseInt(date[2],10), 
    parseInt(date[0],10)-1, 
    parseInt(date[1],10)
  );

}
Community
  • 1
  • 1
savinger
  • 6,544
  • 9
  • 40
  • 57
  • *Always* add the `radix` parameter when using the `parseInt(string [, radix])` function, because all strings beginning with '0' will be interpreted as octal (unless they begins with '0x', then they will be interpreted as hexadecimal). Ref: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/parseInt – matsev Jan 06 '12 at 21:10
  • Nice code, similar to what I have already. But I'm looking for something to give me the actual date as a string like "March 20, 2012". I'd like to use a built in function if there is one instead of running a switch statement on the number for the month. – Proxy404 Jan 06 '12 at 21:12
  • @matsev I certainly will from now on. I went back and edited it immediately upon submission. I actually just learned about this [today](http://stackoverflow.com/questions/8763396/javascript-parseint-with-leading-zeros). – savinger Jan 06 '12 at 21:14
0

By accessing an array you could do this

var monthArray = ['January', 'February', ... , 'December'];


var date = "05/25/2012";

var dateParts = date.split('/');

var convertedDate = monthArray[parseInt(dateParts[0], 10)] + " " + dateParts[1] + ", " + dateParts[2];
John Hartsock
  • 85,422
  • 23
  • 131
  • 146
  • More efficient than a switch statement. I'll use this if i don't find a library function to do it. – Proxy404 Jan 06 '12 at 21:14
  • Just one edit: var convertedDate = monthArray[parseInt(dateParts[0],10)-1] + ... Without the '-1' it will give the month after. Otherwise this works. – Proxy404 Jan 06 '12 at 22:03