52

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?

2

5 Answers 5

49

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>
Sign up to request clarification or add additional context in comments.

Comments

13

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

5 Comments

I tried this: var start_date = Date.parse(startDate).toString("YYYY-MM-DD") (where startDate was 11/30/2015) and got YYYY-11-DD as output.
@pceccon Answer to an old question for future readers, but the format should have been ("yyyy-MM-dd"). The library is case sensitive since you need to distinguish between minutes / month and 12 / 24 hour formats.
Is there a more current equivalent of datejs that someone could recommend? The download URL is dated 2007... :)
Late response but I would recommend date-fns or moment.js
moment.js is deprecated, go with date-fns instead or look for valid alternatives
7
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);

Comments

4

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();

Comments

1

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

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

4 Comments

@MarcB does w3fools have any problem with this w3cschools.com reference for javascript date ? please be specific.
Just a general reaction to seeing them linked here. Just because they're first is google's results doesn't mean they're the best to link to. For JS, it'd better to link to the equivalent MDN entry instead: developer.mozilla.org/en/JavaScript/Reference/Global_Objects/… better docs with much less visual spam.
Heh I use W3CHOOLS for reference all the time, mostly because it's the first thing that always pops up on Google. I'll have to read this now..

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.