2

I want to get the name of the month in JavaScript in capitals.

I know I can use the getMonth() method to get the current month and use the number to extract the month name in capitals from an array, but is there a built-in method to do the same?

Kitty1911
  • 661
  • 1
  • 8
  • 15
  • `var monthNames = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ]; document.write("The current month is " + monthNames[d.getMonth()]);` Of course you can capalize the months in the array or use the toUpperCase function. (I know uses an array but its simple) – Trevor Oct 11 '13 at 18:45
  • 1
    How's this work: http://stackoverflow.com/questions/1643320/get-month-name-from-date-using-javascript -- From the bottom post: http://jsfiddle.net/dstorey/Xgerq/ – tymeJV Oct 11 '13 at 18:45
  • 'but is there a built-in method to do the same'... simple answer, no. while the syntax itself is en-us, the language is culture agnostic. btw agree with Barmar... moment.js is the way to go if you want to power up JS dates. – James Gaunt Oct 11 '13 at 18:47
  • @JamesGaunt maybe JavaScript doesn't have a method built-in, but there has to be a lib out there that does. – Mr Jones Oct 11 '13 at 18:48
  • 3
    Try moment.js, it's the canonical library for date/time-related needs. – Barmar Oct 11 '13 at 18:48
  • @Mr Jones... indeed. but that wouldn't be 'built in' would it? if you can't be bothered to type up a 100 character array why would you include an entire library? – James Gaunt Oct 11 '13 at 18:49
  • @JamesGaunt because it only takes one line of code to include a library – Mr Jones Oct 11 '13 at 18:51
  • check https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toDateString?redirectlocale=en-US&redirectslug=JavaScript%2FReference%2FGlobal_Objects%2FDate%2FtoDateString – Emilio Gort Oct 11 '13 at 18:53
  • I especially like moment.js because it supports localization. An array-based method supporting more than English would be a less trivial exercise. – Brian Anderson Oct 11 '13 at 18:54
  • An easy solution would be to use css -> text-transform: capitalize; – Sir Von Berker Jul 19 '22 at 13:51

6 Answers6

3

You may want to consider the JavaScript Date object's function toLocaleString(). You can specify the locale and format to retrieve.

ErinsMatthew
  • 591
  • 4
  • 11
3

Like this, see toLocaleString

While this method has been around for quite some time, it is only recently that browsers have begun to implement the locales and options arguments and is therefore not yet widely supported.

Javascript

var today = new Date(),
    options = {
        month: "long"
    },
    month = today.toLocaleString("en-GB", options).toUpperCase();

alert(month);

jsFiddle

Xotic750
  • 22,914
  • 8
  • 57
  • 79
1

Javascript does not have a method built in to retrieve the name of the month. You will have to create a method of your own, nearly all of which will use some form of fetching an entry from an Array of month names.

CassOnMars
  • 6,153
  • 2
  • 32
  • 47
  • 1
    The JavaScript Date object does have a series of "to...String()" functions that include the month name, but the way the string is returned for these functions is implementation dependent and may vary by locale as well. – ErinsMatthew Oct 11 '13 at 18:50
0
var d=new Date();
var month=['January' , 'February' ,'March', 'April' , 'May' ,'June', 'July' , 'August' ,'September', 'October' , 'November' ,'December'];
var n = month[d.getMonth()];
alert(n);
0

If you are content with the 3 letter month abbreviation this would work:

d.toDateString().substr(4,3).toUpperCase()

Full disclaimer: I'm not sure if that would be affected by the region or not.

jeschafe
  • 2,683
  • 1
  • 14
  • 13
0
const today = new Date();
const options = {month: 'long'};

today.toLocaleDateString('it-IT', options);

A way for getting the month from a Date Object.