How to get month from string in javascript?
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]);
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);
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"
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"