48

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?

Community
  • 1
  • 1
junaidp
  • 10,801
  • 29
  • 89
  • 137

5 Answers5

47

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>
Mike Christensen
  • 88,082
  • 50
  • 208
  • 326
14

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
pimvdb
  • 151,816
  • 78
  • 307
  • 352
  • 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 Nov 30 '15 at 19:45
  • 2
    @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. –  Oct 09 '17 at 15:58
  • 1
    Is there a more current equivalent of datejs that someone could recommend? The download URL is dated 2007... :) – Drewdavid Sep 14 '18 at 17:42
  • Late response but I would recommend date-fns or moment.js – FreeWorlder May 29 '20 at 11:11
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);
Naftali
  • 144,921
  • 39
  • 244
  • 303
3

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();
ipr101
  • 24,096
  • 8
  • 59
  • 61
2

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

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

rogerlsmith
  • 6,670
  • 2
  • 20
  • 25
  • 7
    [w3fools](http://w3fools.com) – Marc B Nov 07 '11 at 18:21
  • @MarcB does w3fools have any problem with this w3cschools.com reference for javascript date ? please be specific. – Birey Nov 07 '11 at 18:26
  • 1
    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: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date better docs with much less visual spam. – Marc B Nov 07 '11 at 18:27
  • 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.. – Mike Christensen Nov 07 '11 at 18:33