- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-18-2016 12:27 PM
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!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-18-2016 01:45 PM
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';
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-18-2016 01:50 PM
What are you trying to ultimately do?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-18-2016 01:59 PM
If a user has not filled completed a form four days prior to their new hire start date, send an email to their manager and the division director. Continue process until new hire date.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-18-2016 02:59 PM
Let me know if the script posed above from another user works for you!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-19-2016 10:11 AM
Hey Alex,
Welcome to SN DateTime scripting! (Here be dragons.)
I'm glad that you found Mani's script helpful. Mani is awesome. If his script perfectly suits your purposes then you may not need this at all, but I wanted to mention something: We often only want to count hours that fall during business hours, so we need to use a schedule such as the OOTB "8-5 week days excluding holidays" schedule.
DateTime scripting is notoriously error prone and fiddly, so I have created a little utility object to speed coding and protect myself from the common pitfalls:
//@param strDateTime1 must precede @param strDateTime2, or else the output from getDurationSeconds will be zero
var u_durationCalculator = function (strDateTime1, strDateTime2, scheduleId) {
var outerScope = this;
//If they didn't provide a schedule ID default to the 8-5 week days excluding holidays schedule
this.scheduleId = (!JSUtil.nil(scheduleId)) ? scheduleId : '090eecae0a0a0b260077e1dfa71da828';
this.timeZone = gs.getSession().getTimeZone();
this.dt1 = correctStringDateTimeForGMT(strDateTime1);
this.dt2 = correctStringDateTimeForGMT(strDateTime2);
function correctStringDateTimeForGMT(stringDateTime) {
var dt = new GlideDateTime(stringDateTime);
dt.getLocaltime();
var offSetInSeconds = dt.getTZOffset() / 1000; //convert from millis
var multiplier = (offSetInSeconds < 0) ? -1 : 1;
dt.addSeconds(multiplier * offSetInSeconds);
return dt;
}
function getDurationSeconds() {
var dc = new DurationCalculator();
dc.setSchedule(outerScope.scheduleId, outerScope.timeZone);
var ans = dc.calcScheduleDuration(outerScope.dt1.getDisplayValue(), outerScope.dt2.getDisplayValue());
return ans;
}
return {
getDurationSeconds: getDurationSeconds
};
};
//Example usage
var dc = new u_durationCalculator ('2016-02-19 12:00:00', '2016-02-22 09:30:00');
gs.print('Duration Hours:' + dc.getDurationSeconds() / 3600);
//Output
*** Script: Duration Hours:6.5
As you can see, SN provides a DurationCalculator object that I'm just wrapping.
Godspeed,
Trey Carroll