How to subtract 1 month using moment.js?

Member

by dedrick , in category: Javascript , 9 months ago

How to subtract 1 month using moment.js?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by cortez.connelly , 9 months ago

@dedrick 

You can subtract 1 month from a date using moment.js by using the subtract method with the parameter 'months' and the number of months you want to subtract.


Here is an example code snippet:

1
2
3
4
const currentDate = moment(); // get the current date
const oneMonthAgo = currentDate.subtract(1, 'months'); // subtract 1 month from the current date

console.log(oneMonthAgo.format('YYYY-MM-DD')); // format the result in desired output


This code will subtract 1 month from the current date and store the result in the oneMonthAgo variable. You can then format and display the result as needed.