- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-29-2023 07:36 AM
Hello All,
I cannot able to set the duration between two date fields.
Below is the code which I written.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-29-2023 09:09 AM
Hi @Venkat141,
I have add the comment to understand the code.
var gdt1 = new GlideDateTime("28-11-2023 00:00:00");
var gdt2 = new GlideDateTime("21-11-2023 00:00:00");
var diff = gs.dateDiff(gdt1, gdt2, true); // getting difference in milli seconds
var d = Math.floor(diff / (3600 * 24)); // calculate the number of days
var h = Math.floor(diff % (3600 * 24) / 3600); //calculate the number of hours
var m = Math.floor(diff % 3600 / 60); // calculate the number of Mintues
var s = Math.floor(diff % 60); // calculate the number of seconds
//Now got all details like days, hours, mintues and seconds
// to populate the duration field need to be in below format
// 06 12:00:00 --> days hours:minutes:seconds
var time = d + ' ' + h + ':' + m + ':' + s;
//The default date in JavaScript is January 1, 1970, 00:00:00
var dur = new GlideDuration(time) //GlideDuration is convert this format 06 12:00:00 into Date and time format for example : 1969-12-25 00:00:00
//By setting the date and time (1969-12-25 00:00:00) to the duration field type, it calculates the difference between the default date and the date that is entered. The value of this difference is then set in the duration field.
current.work_duration = dur; //setting the value
current.update(); //updating the form
Hard coded date and time to explain in the script, you can use the field name.
If my response helps you to resolve the issue close the question by Accepting solution and hit thumb icon. From Correct answers others will get benefited in future.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-29-2023 07:39 AM
Please check below link for similar solution.
Thanks
Anil Lande
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-29-2023 07:39 AM - edited 11-29-2023 07:46 AM
Can you please help me understand what is the field data-type of 'Work_start'/'Work_end' & 'work_duration'? I mean is it 'Date/Time' for first 2 & 'duration' data-type for last one ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-29-2023 07:58 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-29-2023 08:18 AM - edited 11-29-2023 08:55 AM
My understanding is, once you find the difference between 2 date/time fields, you can directly use the output variable to set the duration. You don't have to use dur.getDisplayValue(), instead just use 'dur' to set the value.
Find below one example which I tried on my PDI -
So following code in your scenario should work assuming you have written your code in BEFORE business-rule -
or if AFTER business rule being used -
Please do mark my response as CORRECT if it solves your issue or HELPFUL as it will help others & will motivate me as well.