【问题标题】:Moment JS: How to get Month Names of Current Year After the Current Month (Inclusive)Moment JS:如何在当前月份(含)之后获取当前年份的月份名称
【发布时间】:2021-07-16 13:28:21
【问题描述】:

我想获取当前月份的名称以及当年未传递的月份名称。例如,当前月份是 7 月,而我们已经离开了当年的 8 月、9 月、10 月、11 月和 12 月,即 2021 年。

以下是我想要得到的输出:

['July', 'August', 'September', 'October', 'November', 'December']

【问题讨论】:

  • 你能显示一些代码吗?到目前为止你做了什么?

标签: javascript momentjs


【解决方案1】:

您无需使用任何第三方插件,只需拥有一个数组并使用Date 对象的getMonth 函数即可。

// Array of all the months
const months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
// Current month. For `getMonth` `January` is zero.
// more here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getMonth
const month = new Date().getMonth();
const remaining = months.slice(month);
console.log(remaining);

【讨论】:

    猜你喜欢
    • 2016-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多