Rob Sestito
Rob Sestito

Reputation: 21

Doing math towards future date

I am trying to figure out how to calculate future date compared to current date. For Example: (think of Deadline as a Date field) - If Deadline (value form) is in the future but <= 12/31 of the current year, “This Year” - If Deadline (value form) is in the future but > 12/31 of the current year, “Future”

So far, I am unable to figure this out within my code.

I need help with var theFuture AND to create a var for "is future but <= 21/31 of current year.

var theFuture = new Date("January 01 2020");
    //theFuture.setDate(today.getDate());


    //Compare the two numbers
    if (dateToCheck < rightNow || dateToCheck == rightNow) {
        theTiming = "Overdue";
        g_form.setValue('u_timing', theTiming);
    }
    else if (dateToCheck >= approaching) {
        theTiming = "Deadline Approaching";
        g_form.setValue('u_timing', theTiming);
    }
    else if (dateToCheck > theFuture){
        theTiming = "Future";
        g_form.setValue('u_timing, theTiming');
    }   
}

So, results should be: When the user selects a date from Deadline, another field called Timing will generate Text. Current, I am able to calculate if the date selected is today or before today, Timing will say "Overdue". Next, if the date selected is greater than today BUT within 180 days, Timing will say "Deadline Approaching". But, to get the rest that I mentioned above, I am stuck.

Upvotes: 2

Views: 55

Answers (1)

Jake Steffen
Jake Steffen

Reputation: 415

We use moment.js for working with dates it makes things a lot easier.

This will tell you if the date selected is today or not:

var iscurrentDate = moment().isSame(dateToCheck, "day");
if(iscurrentDate)
{
}

You can also do a similar thing for year

var iscurrentDate = moment().isSame(dateToCheck, "year");
if(iscurrentDate)
{
}

More info on moment here: https://momentjs.com/docs/

Upvotes: 1

Related Questions