- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-29-2017 12:49 PM
Hi SNC,
I need some assistance please regarding the below script. It's for a five days validation , it works for five days but does not take into account weekends or bank holidays.
Here's the script below and the script include . Please note,not great with scripting, I had help with this script. Appreciate your assistance.
Client Script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
//Type appropriate comment here, and begin script below
var mydate = newValue;
var dtvalid;
var ga = new GlideAjax('daysdiff');
ga.addParam('sysparm_name','date_selection');
ga.addParam('sysparm_sdt',mydate);
ga.getXML(UserDateParse);
function UserDateParse(response)
{
dtvalid = response.responseXML.documentElement.getAttribute("answer");
if (dtvalid < 5) {
g_form.setValue('date',' ');
alert("The date should be atleast 5 days from today ");
}
}
Script Include
var daysdiff= Class.create();
var rec2 = new ArrayUtil();
daysdiff.prototype = Object.extendsObject(AbstractAjaxProcessor, {
date_selection: function() {
var sec1 = this.getParameter('sysparm_sdt');
var gdt = new GlideDateTime();
gdt.addSeconds(gdt.getTZOffset() / 1000);
var sec2 = gdt.getDate();
return DateSelect(sec2, sec1);
}
});
function DateSelect(rec3, rec4)
{
var dif = gs.dateDiff(rec3, rec4, true);
var time = (parseFloat(dif)*1000)/86400000;
gs.log('Today Date>>>>' + rec3);
gs.log('Planned Date>>>>' + rec4);
gs.log('Time Difference>>>' + time);
return time;
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-29-2017 12:54 PM
I would use the DurationCalculator that exists to do the serverside calculation. Take a look here: https://docs.servicenow.com/bundle/istanbul-servicenow-platform/page/script/server-scripting/concept...
And if you run into trouble, just ask.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-29-2017 01:29 PM
Thanks Michael, will look into this option too.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-29-2017 01:47 PM
Michael beat me to it - rather than look at scripting your own calendar, look at schedules: they respect timezones, holidays, etc.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-29-2017 01:48 PM
durationcalculator uses schedules 😃
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-08-2017 07:04 AM
Hi Goran,
Pls are you able to help with the duration calculator. read about it on the wiki but not quite sure where to start.
will it work with a catalog ui policy for dates as well.
I basically just want to exclude weekends .
Many Thanks