If current date is X days before start date

alexcharleswort
Tera Expert

While I would finally call myself mediocre at scripting, i am still very much amateur at date scripting.

I am setting up a workflow where I want an if script to run. I want the if script to say:

if ((current date + 4 days) > current.variables.req_start_date){

              return 'yes';

          }

          return 'no';

    }

Basically, return true if the current date is 4 days or less away from the start date.

I don't really know how to put this into javascript the correct way or if i need to parse the current date out, if the start date on the form needs to be coming from a date field or if it can be a string field.... I'm assuming it has to be a date field.

Also, if anyone has a better way to accomplish this, I am totally open.

Thanks in advance!

1 ACCEPTED SOLUTION

manikorada
ServiceNow Employee
ServiceNow Employee

Alex have the script like:



answer = ifScript();



function ifScript()


{


var date = new GlideDateTime(current.variables.req_start_date);


var currentDate = new GlideDateTime();


if((date.getNumericValue() - currentDate.getNumericValue()) > (4 * 24 * 60 * 60 * 1000) )


{


return 'yes';


}


return 'no';


}


View solution in original post

8 REPLIES 8

manikorada
ServiceNow Employee
ServiceNow Employee

Alex have the script like:



answer = ifScript();



function ifScript()


{


var date = new GlideDateTime(current.variables.req_start_date);


var currentDate = new GlideDateTime();


if((date.getNumericValue() - currentDate.getNumericValue()) > (4 * 24 * 60 * 60 * 1000) )


{


return 'yes';


}


return 'no';


}


Well, I have only put it through a couple tests but it seems to be working splendidly! Thank you!!!



Out of curiosity, would the script stay the same if req_start_date was a date field rather than a string??


If 'req_start_date' is Date Time field then it will work.


If its date field then you need to do something like:



var date = new GlideDateTime(current.variables.req_start_date + ' 01:00:00' );


So Mani I am trying to do the same type of thing. My scenario is the start date can not be BEFORE the Open date of the request.



Would I use the same script and then I want to include an Alert message to tell the customer something like: "Please select a date after today's date." Or something to that effect is that right.



Thanks,


Karen