Need to calculate the difference(In Seconds) between two dates based on a specific schedule.

Khaja Shaik
Tera Contributor

Hi All,

 

I have the below requirement:

Need to calculate the difference(In Seconds) between two dates based on a specific schedule.

Schedule: Monday-Friday 08:00:00 to 17:00:00

 

I am trying below script in my PDI, its working fine for day part(means it's not calculating time on the weekends) but, it is not working for business hours(means it should have to calculate the time between 08:00:00 to 17:00:00)

 

 

Script:

var timerDate = new GlideDateTime('2024-07-25 08:00:00');

var startDate = new GlideDateTime('2024-07-26 17:00:00');

// Initialize the schedule

var schedule = new GlideSchedule('08fcd0830a0a0b2600079f56b1adb9ae'); //sys_id of my schedule

// Calculate the duration according to the schedule

var diffSeconds = schedule.duration(timerDate, startDate); // Convert milliseconds to seconds

var seconds = diffSeconds.getNumericValue() / 1000;

//gs.info("currentDateTime: "+currentDateTime);

gs.info("Hours: "+diffSeconds.getDurationValue()); //.getDurationValue()

gs.info("Seconds: "+seconds);

 

 

Output:

*** Script: Hours: 11:00:00

*** Script: Seconds: 39600

 

If I increase time on the "startDate" as below (From 17 hours to 22 Hours) its calculating that time too, it should not calculate after 17:00:00 hours as per the schedule please see the output.

 

var timerDate = new GlideDateTime('2024-07-25 08:00:00');

var startDate = new GlideDateTime('2024-07-26 22:00:00');

 

Output:

*** Script: Hours: 16:00:00

*** Script: Seconds: 57600

 

Please help me If I am doing anything wrong.

 

Thanks in advance!!

1 REPLY 1

Bhavya11
Kilo Patron

Hi @Khaja Shaik ,

when i tried below script i got same output

 

var timerDate = new GlideDateTime('2024-07-25 08:00:00');
var startDate = new GlideDateTime('2024-07-26 17:00:00');
var schedule = new GlideSchedule('08fcd0830a0a0b2600079f56b1adb9ae'); 
var duration = schedule.duration(timerDate, startDate);
var seconds = duration.getNumericValue() / 1000;
gs.info("Duration in hours: " + duration.getDurationValue());
gs.info("Duration in seconds: " + seconds);

 

 

Output: 

*** Script: Duration in hours: 18:00:00
*** Script: Duration in seconds: 64800

while increasing the DATE 

 

var timerDate = new GlideDateTime('2024-07-25 08:00:00');
var startDate = new GlideDateTime('2024-07-26 22:00:00');
var schedule = new GlideSchedule('08fcd0830a0a0b2600079f56b1adb9ae'); 
var duration = schedule.duration(timerDate, startDate);
var seconds = duration.getNumericValue() / 1000;
gs.info("Duration in hours: " + duration.getDurationValue());
gs.info("Duration in seconds: " + seconds);

 

 

 Output :


*** Script: Duration in hours: 18:00:00
*** Script: Duration in seconds: 64800

 Please mark helpful & correct answer if it's really worthy for you.

 

 

Thanks,

BK