skip to Main Content

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


  1. Chosen as BEST ANSWER

    Switched up the logic behind how a status is being determined and it works fine without usage of the isSameOrAfter method.

            const today = moment.utc();
            const planEndDate = moment.utc(value);
            let content;
            if (today.isAfter(planEndDate, 'day')) {
                content = renderNotification('warning', 'inactive');
            } else {
                content = renderNotification('info', 'active');
            }
    

  2. To use moment.js’s isSameOrAfter, the date should be in format of moment(YYYY-MM-DD)

    so first convert it to that format

    const today = moment().format('YYYY-MM-DD');
            let planEndDate = moment(value).format('YYYY-MM-DD');
            let content;
            if (moment(planEndDate).isSameOrAfter(today)) {
                content = renderNotification('info', 'active');
            } else {
                content = renderNotification('warning', 'inactive');
            }
    
    Login or Signup to reply.
  3. If you want to compare the day, then you need to add the granularity

      const today = moment();
        const planEndDate = moment(value);
        let content;
        if (planEndDate.isSameOrAfter(today, 'day')) { //<--- here
            content = renderNotification('info', 'active');
        } else {
            content = renderNotification('warning', 'inactive');
        }
    

    Else your current code is currently compared by the milliseconds and "inactive" is correct.

    Your below planEndDate is before today if comparing the milliseconds.

    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)

    You May Not Need Moment

    Using date-fns

      var isAfter = require('date-fns/isAfter')
      var isToday = require('date-fns/isToday')
    
      const today = new Date();
      const planEndDate = new Date(value)
    
      if (isAfter(today, planEndDate) || isToday(planEndDate)) {
         content = renderNotification('info', 'active');
      } else {
         content = renderNotification('warning', 'inactive');
      }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search