I have a function as follows it takes in a date and checks whether the given date is in last month or not.
const {
subMonths,
getMonth,
lastDayOfMonth,
startOfMonth,
isWithinInterval
} = require('date-fns')
function isLastMonth(date) {
let lastMonthDateOfGivenDate = subMonths(new Date(date), 1)
let today = Date.now()
let lastMonthDate = subMonths(today, 1);
let firstDayOfLastMonth = startOfMonth(lastMonthDate)
let lastDayOfLastMonth = lastDayOfMonth(lastMonthDate)
if (isWithinInterval(lastMonthDateOfGivenDate, { start: firstDayOfLastMonth, end: lastDayOfLastMonth }))
console.log("true")
else
console.log('false')
}
isLastMonth("2020-03-30T15:24:02.647Z")
The problem is is I'm not able to check for dates which have 31 days or 30 days for february.
Any solution or other approach for this problem? Thank you.