Skip to content

Get previous month from current date #2945

Answered by fturmel
ram027 asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @ram027,

You can try subMonths, or simply set the day of the month to 0 on a date instance to change it to the last day of the previous month.

const date = new Date(2022, 0, 15);

const previousMonth = new Date(date.getTime());
previousMonth.setDate(0);

console.log(date);          // Sat Jan 15 2022 00:00:00 GMT-0500 (Eastern Standard Time)
console.log(previousMonth); // Fri Dec 31 2021 00:00:00 GMT-0500 (Eastern Standard Time)

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by tan75
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants