[javascript] How to change date format in JavaScript

Possible Duplicate:
Formatting a date in javascript

I have this:

HTML

Start Date:  <input type="date" id="startDate" name="startDate" ></p>

JavaScript

var  mydate = new Date(form.startDate.value);

After that mydate becomes

"05/05/2010"

Now, I want to change this format to

May 2010

Is there a way of doing it in JavaScript?

This question is related to javascript datetime

The answer is


Try -

var monthNames = [ "January", "February", "March", "April", "May", "June",
    "July", "August", "September", "October", "November", "December" ];

var newDate = new Date(form.startDate.value);
var formattedDate = monthNames[newDate.getMonth()] + ' ' + newDate.getFullYear();

Use your mydate object and call getMonth() and getFullYear()

See this for more info: http://www.w3schools.com/jsref/jsref_obj_date.asp


var month = mydate.getMonth(); // month (in integer 0-11)
var year = mydate.getFullYear(); // year

Then all you would need to have is an array of months:

var months = ['Jan', 'Feb', 'Mar', ...];

And then to show it:

alert(months[month] + "  " + year);

Using the Datejs library, this can be as easy as:

Date.parse("05/05/2010").toString("MMMM yyyy");
//          parse date             convert to
//                                 string with
//                                 custom format