How to calcuate past days duration using schedule
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-17-2024 09:20 AM
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-17-2024 06:38 PM
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:
- Use the subtract() method of the GlideSchedule class.
- Pass the appropriate duration in milliseconds as a negative GlideDuration to move backward in time.
- 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
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.
Negative GlideDuration:
- To go back in time, use a negative GlideDuration.
subtract() Method:
- The schedule.subtract(startDate, duration) method calculates the past date by respecting the working hours and holidays defined in the schedule.
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).
- If the current date is Dec 18, 2024, 10:00 AM, and the schedule excludes holidays and weekends:
Validation
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.
Use gs.print() to validate the calculated dates.
Adjust the durationInMillis if the working hours differ (e.g., 9 hours instead of 8).
Please check if it works
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-17-2024 07:07 PM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-28-2025 12:54 AM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader