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.
JavaScript Date getMonth() getMonth() returns the month (0 to 11) of a date.
Javascript date getMonth() method returns the month in the specified date according to local time. The value returned by getMonth() is an integer between 0 and 11.
The getMonth() method returns the month in the specified date according to local time, as a zero-based value (where zero indicates the first month of the year).
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With