[英]Get localized month name using native JS
可以使用本機javascript執行此操作以獲取本地化的完整月份名稱。
var objDate = new Date("10/11/2009"),
locale = "en-us",
month = objDate.toLocaleString(locale, { month: "long" });
但這只能獲取給定日期的月份數。 我只想獲得與月份編號相對應的月份名稱。 例如,如果我執行getMonth(2)
它將返回February
。 如何使用本機javascript實現getMonth
(沒有像moment
這樣的庫)?
你已經接近了:
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));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.