简体   繁体   English

如何获得上个月的第 15 天和本月的第 16 天

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

I am trying to get last month of day 15 and current month of day 16 in moment but I am failed could someone please help me how to resolve this issue.我正在尝试获得第 15 天的最后一个月和第 16 天的当前月份,但我失败了,有人可以帮助我如何解决这个问题。

Expected result => 15 oct, 15 Nov 2020预期结果 => 2020 年 11 月 15 日 10 月 15 日

To get last month with date 15 you need this:要获得上个月的日期 15,您需要:

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

You subtract one month and set date to 15. This returns 15 october.您减去一个月并将日期设置为 15。这将返回 10 月 15 日。

To get current date 15, just remove the subtract part.要获得当前日期 15,只需删除减法部分。

To get exactly the result you asked for then:要获得您当时要求的准确结果:

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

Where string is 15 Oct, 15 Nov 2020其中 string 是15 Oct, 15 Nov 2020

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM