如何使用ECMAScript Internationalization API获取所有长月份名称的列表?

例如,如果用户的语言环境是en-US,我想获得以下信息:

["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]

最佳答案

应该这样做:

function getMonthsForLocale(locale) {
    var format = new Intl.DateTimeFormat(locale, { month: 'long' })
    var months = []
    for (var month = 0; month < 12; month++) {
        var testDate = new Date(Date.UTC(2000, month, 1, 0, 0, 0));
        months.push(format.format(testDate))
    }
    return months;
}

关于javascript - 如何使用Intl在javascript中获取月份名称列表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47232534/

10-12 12:29
查看更多