简体   繁体   中英

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.

Expected result => 15 oct, 15 Nov 2020

To get last month with date 15 you need this:

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

You subtract one month and set date to 15. This returns 15 october.

To get current date 15, just remove the subtract part.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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