2

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.

DjaouadNM
  • 22,013
  • 4
  • 33
  • 55

1 Answers1

1

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/

Jake Steffen
  • 415
  • 2
  • 11
  • moment() is the current date – Jake Steffen Jun 06 '19 at 18:17
  • Thanks @Jake Steffen - however it looks like I need to figure out how to use moment() within ServiceNow. I thought it would be moment.js - but I get a ReferenceError: moment is not defined function () {[native code]} – Rob Sestito Jun 06 '19 at 19:05
  • Is servicenow some type of content management system? this might help: https://community.servicenow.com/community?id=community_question&sys_id=f9a898ebdb0033c0f7fca851ca9619e8 – Jake Steffen Jun 06 '19 at 19:11
  • Yes it is - ServiceNow is our Case Management System. I actually found that same post you just shared - looking at that now! – Rob Sestito Jun 06 '19 at 19:14
  • so far after following their instructions, it does not work for me. I am trying to figure out if this is also made for the application I am using within ServiceNow. – Rob Sestito Jun 06 '19 at 19:43