Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
30
rated 0 times [  34] [ 4]  / answers: 1 / hits: 21177  / 8 Years ago, tue, october 11, 2016, 12:00:00

It's possible to do this to get the localized full month name using native .



var objDate = new Date(10/11/2009),
locale = en-us,
month = objDate.toLocaleString(locale, { month: long });


But this only gets the month number for a given date. I'd simply like to get the month name corresponding to a month number. For example, if I do getMonth(2) it would return February. How can I implement getMonth using native (no libraries like moment)?


More From » date

 Answers
7

You are already close:





var getMonth = function(idx) {

var objDate = new Date();
objDate.setDate(1);
objDate.setMonth(idx-1);

var locale = en-us,
month = objDate.toLocaleString(locale, { month: long });

return month;
}

console.log(getMonth(1));
console.log(getMonth(12));




[#60439] Friday, October 7, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
analiseb

Total Points: 252
Total Questions: 96
Total Answers: 106

Location: Singapore
Member since Sat, Jul 25, 2020
4 Years ago
analiseb questions
;