如何即时获取上月第15天和本月第16天

How to get last month day 15 and current month day 16 in moment

我正在尝试立即获取上个月的第 15 天和当前月份的第 16 天,但我失败了,有人可以帮助我解决这个问题吗?

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

要获取日期为 15 的上个月,您需要这样做:

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

你减去一个月并将日期设置为 15。这个 returns 10 月 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