I’m currently trying to switch a status to active
if the end date of a plan is the same as or after today’s date. This is what I currently have:
const today = moment();
const planEndDate = moment(value);
let content;
if (planEndDate.isSameOrAfter(today)) {
content = renderNotification('info', 'active');
} else {
content = renderNotification('warning', 'inactive');
}
This is the value of planEndDate: Tue Mar 28 2023 11:00:00 GMT+1100 (Australian Eastern Daylight Time)
.
And this is the value of today: Tue Mar 28 2023 15:03:04 GMT+1100 (Australian Eastern Daylight Time)
I’m trying to figure out why I’m getting an inactive
status for my data when I’m expecting it to be active
when using the isSameOrAfter
method.
3
Answers
Switched up the logic behind how a status is being determined and it works fine without usage of the
isSameOrAfter
method.To use moment.js’s
isSameOrAfter
, the date should be in format ofmoment(YYYY-MM-DD)
so first convert it to that format
If you want to compare the day, then you need to add the granularity
Else your current code is currently compared by the milliseconds and "inactive" is correct.
Your below planEndDate is before today if comparing the milliseconds.
You May Not Need Moment
Using date-fns