JavaScript<\/a> provides a couple of ways to print out the current month by name. We shall show you two ways.<\/p>\n\n\n\nOption 1: Display Current Month by Name From Array<\/h2>\n\n\n\n Here we first create an array with all the month names, then we will use the index as a key to finding the month we want.<\/p>\n\n\n\n
Create an array with all the month names, and so use your index as a key to finding the current month as shown here.<\/p>\n\n\n\n
var monthNames = ["January", "February", "March", "April", "May","June","July", "August", "September", "October", "November","December"];<\/code><\/pre>\n\n\n\n<\/p>\n\n\n\n
Next, we can use the JavaScript Date()<\/em> object to retrieve the current month then use the result as the index of the array as follows.<\/p>\n\n\n\nvar d = new Date();\nconsole.log("The current month is " + monthNames[d.getMonth()]);<\/code><\/pre>\n\n\n\n<\/p>\n\n\n\n
The code above outputs the current month. The method above is not very portable and is error-prone as it relies heavily on what you put into the array.<\/p>\n\n\n\n
Option 2: Using the Built-In toLocaleDateString<\/em> Method<\/h2>\n\n\n\nThe JavaScript toLocaleDateString<\/em> method can be used to show parts of a date in your preferred format. It returns a string with a language-sensitive representation of the date portion of this date.<\/p>\n\n\n\nThe new locales and options arguments let applications specify the language whose formatting conventions should be used and allow to customize the behavior of the function. In older implementations, which ignore the locales and options arguments, the locale used and the form of the string returned are entirely implementation-dependent.<\/p>\n\n\n\n
var event = new Date();\n\nconsole.log(event.toLocaleDateString('de-DE', { month: 'long'}));\n\/\/ expected output: Februar\n\nconsole.log(event.toLocaleDateString('ar-EG', { month: 'long'}));\n\/\/ expected output: \u0641\u0628\u0631\u0627\u064a\u0631\n\nconsole.log(event.toLocaleDateString('en-US', { month: 'long'}));\n\/\/ expected output: February\n\nconsole.log(event.toLocaleDateString(undefined, { month: 'long'}));\n\/\/ expected output: February (varies according to default locale)\n\nconsole.log(event.toLocaleDateString(undefined, { month: 'short'}));\n\/\/ expected output: Feb (varies according to default locale)<\/code><\/pre>\n\n\n\n<\/p>\n\n\n\n
To display a specific month based on a different date you can replace the above and use the UTC()<\/em> method like so:<\/p>\n\n\n\nevent = new Date(Date.UTC(2021, 11, 16, 3, 0, 0));<\/code><\/pre>\n\n\n\n<\/p>\n\n\n\n
Ref: [ 1<\/a> ]<\/p>\n","protected":false},"excerpt":{"rendered":"This is how you can get and display the current month by name using the JavaScript toLocaleDateString method in your web projects. JavaScript provides a couple of ways to print…<\/p>\n","protected":false},"author":1,"featured_media":9628,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,23,9,27,16],"tags":[212,303,328,523,600,635,638],"yoast_head":"\n
How to Display the Current Month by Name in JavaScript<\/title>\n \n \n \n \n \n \n \n \n \n \n \n \n \n\t \n\t \n\t \n \n \n \n \n \n\t \n\t \n\t \n