How to find business days between 2 dates from form through client script ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-14-2022 04:37 AM
How to find business days between 2 dates from form through client script ?
It must exclude the weekends. Note - This needs to be achieved from client script when start date and end dates are filled and immediately the duration field below it must populate the days
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-14-2022 05:01 AM
Hi @Snehal13
For that you need to first have a Schedule in [cmn_schedule] table which will exclude weekends and if any other holidays and include only business days. (You will get many OOTB Schedules in the table which you can refer and reuse or can create your own).
Now you can use GlideSchedule() API to calculate duration which is Server Side API so you have to use Script Include.
You can pass the values of your 2 dates as parameters for this Script Include Function. Than can use below code to calculate duration:
var startDate = new GlideDateTime(start_Date); // 1st Date parameter
var endDate = new GlideDateTime(end_date); //2nd Date parameter
var schedule = new GlideSchedule();
schedule.load('090eecae0a0a0b260077e1dfa71da828'); // sys_id of your Schedule you created for Business Days
var duration = schedule.duration(startDate, endDate);
return duration.getDurationValue(); // gets the elapsed time in schedule
Thanks
Anubhav Ritolia
ServiceNow Rising Star 2023
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-18-2022 08:16 AM
Is there away to calculate days between 2 dates in a client script ? I would like to avoid using a server side script.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-18-2022 09:41 AM
Hi @Snehal13
This solution is for Client Script only using Script Include. You have to right this code in Script Include and call it from client script.
I don't think we can check through client side scripting.
Thanks
Anubhav Ritolia
ServiceNow Rising Star 2023
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-18-2022 10:19 PM
exclude weekend ?