Skip to page contentSkip to chat
ServiceNow support
    • Community
      Ask questions, give advice, and connect with fellow ServiceNow professionals.
      Developer
      Build, test, and deploy applications
      Documentation
      Find detailed information about ServiceNow products, apps, features, and releases.
      Impact
      Accelerate ROI and amplify your expertise.
      Learning
      Build skills with instructor-led and online training.
      Partner
      Grow your business with promotions, news, and marketing tools
      ServiceNow
      Learn about ServiceNow products & solutions.
      Store
      Download certified apps and integrations that complement ServiceNow.
      Support
      Manage your instances, access self-help, and get technical support.
How to calculate the duration between 2 days with Scheduler function in Script - Support and Troubleshooting
  • >
  • Knowledge Base
  • >
  • Support and Troubleshooting (Knowledge Base)
  • >
  • How to calculate the duration between 2 days with Scheduler function in Script
KB0823046

How to calculate the duration between 2 days with Scheduler function in Script


4182 Views Last updated : Jul 23, 2025 public Copy Permalink
KB Summary by Now Assist

Summary

This article is providing the way to calculate the duration of days by using Scheduler function in Script.

With Scheduler, it is easy to calculate the duration with some option, like exclude week-end or holiday.

Instructions

You can calculate the duration of 2 days in the next step:

  1. Open Scheduler ("System Scheduler -> Schedules -> Schedules")
  2. Open the schedule you want to use. In this example, using "8-5 weekdays" to calculate exclude weekend.
  3. Check the option : All Day. If this is not checked, the calculation is consider only business hour set in the scheduler.
  4. Prepare the script. Please refer the below sample.

If you need to calculate exclude holiday, please use the schedule: "8-5 weekdays excluding holidays"

 

Sample

var startDate = new GlideDateTime('2020-4-17 00:00:00');
var endDate = new GlideDateTime('2020-4-21 00:00:00');
var schedule = new GlideSchedule();

schedule.load('08fcd0830a0a0b2600079f56b1adb9ae'); // loads "8-5 weekdays" schedule. ('08fcd0830a0a0b2600079f56b1adb9ae' is sysid of the schedule.)
var duration = schedule.duration(startDate, endDate);
gs.print(duration.getDurationValue()); // gets the elapsed time in schedule

 

Result of the code

18:00:00


The world works with ServiceNow.

Sign in for more! There's more content available only to authenticated users Sign in for more!
Did this KB article help you?
Did this KB article help you?

How would you rate your Now Support digital experience?

*

Very unsatisfied

Unsatisfied

Neutral

Satisfied

Very satisfied

Very unsatisfied

Unsatisfied

Neutral

Satisfied

Very satisfied

What can we improve? Please select all that apply.

What are we doing well? Please select all that apply.

Tell us more

*

Do you expect a response from this feedback?

  • Terms and conditions
  • Privacy statement
  • GDPR
  • Cookie policy
  • © 2025 ServiceNow. All rights reserved.