months()/weekdays()


moment.months()
moment.monthsShort()
moment.weekdays()
moment.weekdaysShort()
moment.weekdaysMin()

有时需要在语言环境中获取月份或工作日的列表,例如当填充下拉菜单时。

moment.months();

返回当前语言环境中的月份列表。

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

同样,moment.monthsShort 返回缩写的月份名称,moment.weekdaysmoment.weekdaysShortmoment.weekdaysMin 返回工作日的列表。

可以将整数传给每个函数以获取特定的月份或工作日。

moment.weekdays(3); // 'Wednesday'

2.13.0 开始,可以将布尔值作为 weekday 函数的第一个参数传入。 如果为 true,则将会按特定于语言环境的顺序返回工作日。 例如,在阿拉伯语言环境中,星期六是一周的第一天,因此:

moment.locale('ar');
moment.weekdays(true); // 以阿拉伯语列出周六至周五的工作日列表
moment.weekdays(true, 2); // 将会以阿拉伯语返回星期一

注意:如果缺少语言环境特定的参数,则无论星期几是本地的第一天,工作日始终将星期日作为索引 0。

当格式化月份名称时,某些语言环境会特别考虑。 例如,荷兰语格式化月份的缩写时不带末尾的句号,但前提是格式化破折号之间的月份。 months 方法支持传入格式,以便在适当的上下文中列出月份。

moment.locale('nl');
moment.monthsShort(); // ['jan.', 'feb.', 'mrt.', ...]
moment.monthsShort('-MMM-'); // [ 'jan', 'feb', 'mrt', ...]

最后,可以结合使用格式选项和整数选项。

moment.monthsShort('-MMM-', 3); // 'apr'
moment.months()
moment.monthsShort()
moment.weekdays()
moment.weekdaysShort()
moment.weekdaysMin()

It is sometimes useful to get the list of months or weekdays in a locale, for example when populating a dropdown menu.

moment.months();

Returns the list of months in the current locale.

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

Similarly, moment.monthsShort returns abbreviated month names, and moment.weekdays, moment.weekdaysShort, moment.weekdaysMin return lists of weekdays.

You can pass an integer into each of those functions to get a specific month or weekday.

moment.weekdays(3); // 'Wednesday'

As of 2.13.0 you can pass a bool as the first parameter of the weekday functions. If true, the weekdays will be returned in locale specific order. For instance, in the Arabic locale, Saturday is the first day of the week, thus:

moment.locale('ar');
moment.weekdays(true); // lists weekdays Saturday-Friday in Arabic
moment.weekdays(true, 2); //will result in Monday in Arabic

Note: Absent the locale specific parameter, weekdays always have Sunday as index 0, regardless of the local first day of the week.

Some locales make special considerations into account when formatting month names. For example, Dutch formats month abbreviations without a trailing period, but only if it's formatting the month between dashes. The months method supports passing a format in so that the months will be listed in the proper context.

moment.locale('nl');
moment.monthsShort(); // ['jan.', 'feb.', 'mrt.', ...]
moment.monthsShort('-MMM-'); // [ 'jan', 'feb', 'mrt', ...]

And finally, you can combine both the format option and the integer option.

moment.monthsShort('-MMM-', 3); // 'apr'