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.

4

9 Answers 9

20

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

1 Comment

Seems to be supported on every modern browser caniuse.com/#search=internationalization
18

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

Comments

10

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"

2 Comments

He wanted the months name
Superb . Really helped me
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.

Comments

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

Comments

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

Comments

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

Comments

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...

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.