【问题标题】:Get month name of last month using moment使用时刻获取上个月的月份名称
【发布时间】:2017-02-15 09:37:30
【问题描述】:

我使用以下代码获取最近几个月的 startDate 和 endDate。

// Previous month
var startDateMonthMinusOne = moment().subtract(1, "month").startOf("month").unix();
var endDateMonthMinusOne   = moment().subtract(1, "month").endOf("month").unix();

// Previous month - 1

var startDateMonthMinusOne = moment().subtract(2, "month").startOf("month").unix();
var endDateMonthMinusOne   = moment().subtract(2, "month").endOf("month").unix();

如何才能获得月份名称? (一月,二月,...)

【问题讨论】:

标签: javascript momentjs


【解决方案1】:

使用format() 函数代替unix() 来格式化日期时间,使用MMMM 格式说明符作为月份名称。

var monthMinusOneName =  moment().subtract(1, "month").startOf("month").format('MMMM');

见章节Display / Format in the documentation

【讨论】:

    【解决方案2】:

    您可以简单地使用format('MMMM')

    这是一个工作示例:

    var currMonthName  = moment().format('MMMM');
    var prevMonthName  = moment().subtract(1, "month").format('MMMM');
    
    console.log(currMonthName);
    console.log(prevMonthName);
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多