How to change date format in JavaScript [duplicate]
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?
You can certainly format the date yourself..
var mydate = new Date(form.startDate.value);
var month = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"][mydate.getMonth()];
var str = month + ' ' + mydate.getFullYear();
You can also use an external library, such as DateJS.
Here's a DateJS example:
<script src="http://www.datejs.com/build/date.js" type="text/javascript"></script>
<script>
var mydate = new Date(form.startDate.value);
var str = mydate.toString("MMMM yyyy");
window.alert(str);
</script>
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
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);