【问题标题】:How to get last month day 15 and current month day 16 in moment如何立即获得上个月第 15 天和当前月第 16 天
【发布时间】:2021-02-17 01:01:37
【问题描述】:

我正在尝试获取第 15 天的最后一个月和第 16 天的当前月份,但我失败了,有人可以帮我解决这个问题。

预期结果 => 2020 年 10 月 15 日,11 月 15 日

【问题讨论】:

    标签: javascript reactjs ecmascript-6 momentjs


    【解决方案1】:

    要获得日期为 15 的上个月,您需要:

    moment().subtract(1, 'month').date(15);
    

    您减去一个月并将日期设置为 15。这将返回 15 十月。

    要获得当前日期 15,只需删除减法部分。

    为了得到你所要求的结果:

    const currentMonthDate15 = moment().date(15);
    const lastMonthDate15 = moment().date(15).subtract(1, 'month');
    const string = lastMonthDate15.format('DD MMM') + ', ' + currentMonthDate15.format('DD MMM YYYY');
    

    字符串是15 Oct, 15 Nov 2020

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-02-25
      • 1970-01-01
      • 1970-01-01
      • 2021-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多