;

How to Get a Month Name from a Month Number or by using a Specific date in javascript or JQuery


Tutorialsrack 08/04/2020 Jquery Javascript

In this article, we will learn how to get a month's name from a month number or by using a specific date in javascript or jquery. In javascript month numbers start from 0 to 11 such as 0 equals to January, 11 equals to December. There are various ways to get the month name from a month number or by using a specific date in javascript or jquery. Here are some examples as follows:

Example 1: In this example, we will create a method to get the full month name from the month number.

Example 1
function GetMonthName(monthNumber) {
      var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
      return months[monthNumber - 1];
}

console.log("Month Name: "+GetMonthName(12))
// Output ==> Month Name: December

console.log("Month Name: "+GetMonthName(1))
// Output ==> Month Name: January

console.log("Month Name: "+GetMonthName(2))
// Output ==> Month Name: February

console.log("Month Name: "+GetMonthName(7))
// Output ==> Month Name: July

Example 2: In this example, we will create a method to get a short month name or month abbreviation from month number.

Example 2
function GetMonthName_abbvr(monthNumber) {
	var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec'];
	return months[monthNumber - 1];
}

console.log("Month Name: "+GetMonthName_abbvr(12))
// Output ==> Month Name: Dec

console.log("Month Name: "+GetMonthName_abbvr(1))
// Output ==> Month Name: Jan

console.log("Month Name: "+GetMonthName_abbvr(2))
// Output ==> Month Name: Feb

console.log("Month Name: "+GetMonthName_abbvr(7))
// Output ==> Month Name: Jul

Example 3: In this example, we are using a Date Object to get the full month name and short month name or month abbreviation using a specific date.

Example 3
const fullMonthNames = ["January", "February", "March", "April", "May", "June",
  "July", "August", "September", "October", "November", "December"
];

const shortMonthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
  "Jul", "Aug", "Sept", "Oct", "Nov", "Dec"];

// Get Full Month Name from using Current Date
const d = new Date();
console.log("Full Month Name: " + fullMonthNames[d.getMonth()]);
// Output ==> Full Month Name: April

//********Get Full Month Name********//
// Get Full Month Name from using Specific Date
const d1 = new Date(2020,11,25);
console.log("Full Month Name: " + fullMonthNames[d1.getMonth()]);
// Output ==> Full Month Name: December

const d2 = new Date(2018,0,25);
console.log("Full Month Name: " + fullMonthNames[d2.getMonth()]);
// Output ==> Full Month Name: January

//********Get Short Month Name********//
// Get Short Month Name from using Current Date
const d3 = new Date();
console.log("Short Month Name: " + shortMonthNames[d3.getMonth()]);
// Output ==> Full Month Name: Apr

// Get Short Month Name from using Specific Date
const d4 = new Date(2020,11,25);
console.log("Short Month Name: " + shortMonthNames[d4.getMonth()]);
// Output ==> Full Month Name: Dec

const d5 = new Date(2018,0,25);
console.log("Short Month Name: " + shortMonthNames[d5.getMonth()]);
// Output ==> Full Month Name: Jan

Example 4: In this example, we will use the toLocaleString() method, Every Date object instance has a toLocaleString() method, by using this method you can get the month name in your current locale. Here are some examples to use this method as follows:

Example 4.1: To get the Full Month Name using Date

Example 4.1
const today = new Date();
var fullMonthname = today.toLocaleString('default', { month: 'long' });
console.log("Full Month Name: "+fullMonthname);
// Output ==> Full Month Name: April

Example 4.2: To get the Short Month Name using Date

Example 4.2
const today = new Date();
var shortMonthName = today.toLocaleString('default', { month: 'short' });
console.log("Short Month Name: "+shortMonthName);
// Output ==> Full Month Name: Apr

Example 4.3: To get the Culture-Specific Month Name from date, just replace the “default” with the locale you want, for example, "it-IT" will return you “Aprile”.

Example 4.3
const today = new Date();

//Culture Specific Month Name from date
var shortMonthName = today.toLocaleString('it-IT', { month: 'long' });
console.log("Culture Specific Full Month Name from date: "+shortMonthName);
// Output ==> Culture Specific Full Month Name from date: aprile

Another way to get a month's name from a month number or by using a specific date by using third party libraries like Date.js and Moment.js

I hope this article will help you to understand how to get a month's name from a month's number or by using a specific date in javascript or jquery. 

Share your valuable feedback, please post your comment at the bottom of this article. Thank you! 


Related Posts



Comments

Recent Posts
Tags