简体   繁体   English

使用原生 JS 获取本地化月份名称

[英]Get localized month name using native JS

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 .例如,如果我执行getMonth(2)它将返回February How can I implement getMonth using native (no libraries like moment )?如何使用本机实现getMonth (没有像moment这样的库)?

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));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM