- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-23-2017 04:27 AM
Hi Team,
We have a requirement as follows:
We need to populate a date in date field in catalog item by using the following logic:
i.e. current date + 4 business days
Please let me know how can we accomplish this requirement.
Thanks & Regards,
Keerthi
Solved! Go to Solution.
- 5,442 Views

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-24-2017 12:23 AM
Put this is your default value field:
(function getDateWeekDaysInFuture(addDays) {
var sdt = new GlideDateTime();
while (addDays--) {
if (isWeekend()) {
gs.print('weekend');
addDays++;
} else {
gs.print(sdt.getDate());
}
sdt.addDaysLocalTime(1);
}
return sdt.getDate();
function isWeekend() {
return(sdt.getDayOfWeekLocalTime() == '6' || sdt.getDayOfWeekLocalTime() == '7' );
}
})(4);
This covers all 3 cases:
- Starting date is on a weekend
- +4 results on a weekend
- +4 is during a weekend
Does not cover public holidays!
On 24/08:
15:22:35.141: 2017-08-24 |
15:22:35.142: 2017-08-25 |
15:22:35.142: weekend |
15:22:35.142: weekend |
15:22:35.142: 2017-08-28 |
15:22:35.142: 2017-08-29 |
Remove print statements for production!
ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-08-2019 09:00 AM
How would you go about adding half of a day?
Let's say 1 day is 8h (9-5) meaning 0.5 days is 4h. Could the function take 0.5?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-25-2019 03:11 AM
It worked... Thanks a lot.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-25-2019 03:11 AM
It worked... Thanks a lot.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-19-2020 12:49 AM
Hi,
This is an excellent help.
I need little assistance in the code.
i.e., I have a scenario to run this code for multiple 'addDays' variables.
Like the first run for four days, then run for seven days later for 10days.
Thanks,
Vivek