How to calcuate past days duration using schedule

pavi12
Tera Contributor

Hi,

 

 var currDate = new GlideDateTime();

        var closeDate = new GlideDateTime(txt);

        var dur = new GlideDuration(60 * 60 * 9 * 1000 * 3);

        var schedule = new GlideSchedule('sys id'); // this schedule explains the 8-5 weekdays excluding holidays 

        var endDate = schedule.add(closeDate, dur);

 

Like this How can calcuate the past three days from the current date that past days need to validate the above mentioned schedule also?

3 REPLIES 3

Raj_Nishant92
Tera Contributor

Hi @pavi12 

 

To calculate the past three working days (based on a defined schedule) from the current date using the GlideSchedule API, follow these steps:

Explanation

The GlideSchedule API is used to handle schedules that account for working hours and holidays. To calculate the past three working days:

  1. Use the subtract() method of the GlideSchedule class.
  2. Pass the appropriate duration in milliseconds as a negative GlideDuration to move backward in time.
  3. Ensure the schedule is validated for business hours and holidays.

Code Implementation

Here's how you can achieve this:

var currDate = new GlideDateTime(); // Current date and time
gs.print("Current Date: " + currDate);

var schedule = new GlideSchedule('<sys_id_of_schedule>'); // Replace with your schedule's sys_id

// Define the duration for 3 working days
// 1 day = 8 working hours * 60 minutes * 60 seconds * 1000 milliseconds
var durationInMillis = 8 * 60 * 60 * 1000 * 3; // 3 working days
var dur = new GlideDuration(-durationInMillis); // Negative duration to subtract days

// Calculate the past date based on the schedule
var pastDate = schedule.subtract(currDate, dur);

gs.print("Past Date (3 working days ago): " + pastDate);

Key Points

  1. GlideSchedule Constructor:

    • The new GlideSchedule('<sys_id>') requires the sys_id of the schedule.
    • The schedule should define working hours (e.g., 8:00 AM - 5:00 PM) and holidays.
  2. Negative GlideDuration:

    • To go back in time, use a negative GlideDuration.
  3. subtract() Method:

    • The schedule.subtract(startDate, duration) method calculates the past date by respecting the working hours and holidays defined in the schedule.
  4. Output Example:

    • If the current date is Dec 18, 2024, 10:00 AM, and the schedule excludes holidays and weekends:
      • The past date will be Dec 13, 2024, 10:00 AM (if there are no holidays).

Validation

  1. Test in a ServiceNow instance with a schedule that defines:

    • 8:00 AM - 5:00 PM working hours.
    • Weekends as non-working days.
    • Any specific holidays.
  2. Use gs.print() to validate the calculated dates.

  3. Adjust the durationInMillis if the working hours differ (e.g., 9 hours instead of 8).


Please check if it works

Ankur Bawiskar
Tera Patron
Tera Patron

@pavi12 

check these links

Calculate Date By Adding or Subtracting Time With A Schedule 

subtract time from schedule to update task due date 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@pavi12 

Hope you are doing good.

Did my reply answer your question?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader