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?
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?
You may want to consider the JavaScript Date object's function toLocaleString()
. You can specify the locale and format to retrieve.
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);
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.
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);
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.
const today = new Date();
const options = {month: 'long'};
today.toLocaleDateString('it-IT', options);
A way for getting the month from a Date Object.