本文介紹了使用原生 JS 獲取本地化月份名稱的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!
問(wèn)題描述
可以使用本機(jī) javascript.
var objDate = new Date("10/11/2009"),
locale = "en-us",
month = objDate.toLocaleString(locale, { month: "long" });
但這只會(huì)獲取給定日期的月份數(shù).我只想獲取與月份編號(hào)相對(duì)應(yīng)的月份名稱.例如,如果我執(zhí)行 getMonth(2)
,它將返回 February
.如何使用本機(jī) javascript 實(shí)現(xiàn) getMonth
(沒(méi)有像 moment
這樣的庫(kù))?
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 javascript(no libraries like moment
)?
推薦答案
你已經(jīng)很接近了:
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));
這篇關(guān)于使用原生 JS 獲取本地化月份名稱的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!