15

I have tried

     var d=new Date("2012-07-01 00:00:00.0");
     alert(d.getMonth());   

But getting NAN.

I want month as July for the above date.

Edward
  • 1,367
  • 8
  • 26
  • 43
  • try this string= "2012-07-01 00:00:00". Could be that the last '.0' is causing the pare problem – PoeHaH Sep 03 '12 at 10:55
  • 1
    see http://stackoverflow.com/questions/476105/how-can-i-convert-string-to-datetime-with-format-specification-in-javascript if you want to parse in that format – lc. Sep 03 '12 at 10:57
  • try dis one http://stackoverflow.com/questions/1643320/get-month-name-from-date-using-javascript – Bat_Programmer Sep 03 '12 at 11:13
  • Possible duplicate of [Get month name from Date](http://stackoverflow.com/questions/1643320/get-month-name-from-date) – Xakep Jan 10 '17 at 14:14

9 Answers9

19

Assuming your date is in YYYY-MM-DD format

var arr = "2012-07-01 00:00:00.0".split("-");
var months = [ "January", "February", "March", "April", "May", "June",
    "July", "August", "September", "October", "November", "December" ];
var month_index =  parseInt(arr[1],10) - 1;
console.log("The current month is " + months[month_index]);
Zack
  • 158
  • 1
  • 5
Clyde Lobo
  • 9,126
  • 7
  • 34
  • 61
19

Using the JavaScript Internationalization API:

var date = new Date("2012-07-01");

var monthName = new Intl.DateTimeFormat("en-US", { month: "long" }).format;
var longName = monthName(date); // "July"

var shortMonthName = new Intl.DateTimeFormat("en-US", { month: "short" }).format;
var shortName = shortMonthName(date); // "Jul"
danielnixon
  • 4,178
  • 1
  • 27
  • 39
9

Try this:

    var monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
    var str="2012-07-01";   //Set the string in the proper format(best to use ISO format ie YYYY-MM-DD or YYYY-MM-DDTHH:MM:SS)
    var d=new Date(str);  //converts the string into date object
    var m=d.getMonth(); //get the value of month
    console.log(monthNames[m]) // Print the month name

NOTE: The getMonth() returns the value in range 0-11.

Another option is to use toLocaleString

var dateObj = new Date("2012-07-01");
//To get the long name for month
var monthName = dateObj.toLocaleString("default", { month: "long" }); 
// monthName = "November"

//To get the short name for month
var monthName = dateObj.toLocaleString("default", { month: "short" });
// monthName = "Nov"
heretolearn
  • 6,387
  • 4
  • 30
  • 53
3

use...

const month = new Date("2012-07-01 00:00:00.0").toLocaleString('en-US', { month: 'long' });
const day = new Date("2012-07-01 00:00:00.0").toLocaleString('en-US', { day: '2-digit' });

This will make your day happy in easiest way.

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
1

You will need to create an array for it: http://www.w3schools.com/jsref/jsref_getmonth.asp

also why not initialise your date as: var d = new Date(2012,7,1);

lebryant
  • 351
  • 2
  • 18
1

Javascript Date object doesn't store full names of month. So you have to use an array.

var dateString = "2012-07-01 00:00:00.0";

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

var date = new Date(dateString.replace(" ", "T"));
alert(monthNames [date.getMonth()]);
Diode
  • 24,570
  • 8
  • 40
  • 51
0

Try with this:

var months = [ "January", "February", "March", "April", "May", "June",
   "July", "August", "September", "October", "November", "December" ];
document.write("The current month is " + months[d.getMonth()]);

January->0, February-1 and so on...

Rostyslav Dzinko
  • 39,424
  • 5
  • 49
  • 62
david9
  • 169
  • 9
0

You can use this library to make things easier. http://www.datejs.com/ And then try the following code:

var d=new Date.parse("2012-07-01 00:00:00.0");
 alert(d..tString('MMMM'));
Bat_Programmer
  • 6,717
  • 10
  • 56
  • 67
0

Short answer: with the above code most people will get an alert with 6 these days because Chrome and Firefox now understand that format. It's 6 and not 7 because Dates understand months as indexes beginning with 0, so 0 is Jan, 1 is Feb, 2 is Mar, etc. . So the simplest fix is this:

var d=new Date("2012-07-01 00:00:00.0");
var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
var monthIndex = d.getMonth();
alert(months[monthIndex]);

(Please consider using a different approach to constructing dates though!)

Long answer:

var d=new Date("2012-07-01 00:00:00.0");

The problem is you are getting an invalid date object because the browser doesn't understand that string. Browsers aren't consistent about this - Chrome and Firefox actually parse it correctly and will alert 6 instead of NaN. It's still not a good idea to be imprecise with your formats though - current Safari for example doesn't parse it. Formats that are guaranteed to work are described in https://www.rfc-editor.org/rfc/rfc2822#section-3.3.

You might prefer to use this constructor though:

new Date(year, month[, date[, hours[, minutes[, seconds[, milliseconds]]]]]);

In your example, you would call it like so:

var d=new Date(2012, 6, 1);

Note the 6 and not 7 though! Dates understand months as indexes beginning with 0, so 0 is Jan, 1 is Feb, 2 is Mar, etc. . This is confusing when using the date constructor but easier to convert to July that was asked for:

var d=new Date(2012, 6, 1);
var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
var monthIndex = d.getMonth();
alert(months[monthIndex]);

You can learn more about Date here: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Date

Community
  • 1
  • 1
voltrevo
  • 9,870
  • 3
  • 28
  • 33