- 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 08:05 AM
Hi @Venkat141,
To get the difference between the two date and populate the duration in the field which has duration field type. Try to use below script it worked for me.
(function executeRule(current, previous /*null when async*/) {
// Add your code here
var startDate = current.work_start;
var endDate = current.work_end;
var gdt1 = new GlideDateTime(startDate);
var gdt2 = new GlideDateTime(endDate);
var diff = gs.dateDiff(gdt1, gdt2, true);
var d = Math.floor(diff / (3600 * 24));
var h = Math.floor(diff % (3600 * 24) / 3600);
var m = Math.floor(diff % 3600 / 60);
var s = Math.floor(diff % 60);
var time = d + ' ' + h + ':' + m + ':' + s;
var dur = new GlideDuration(time)
current.work_duration = dur;
})(current, previous);
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 08:20 AM
Hello @Mohan raj ,
I tried above code and it's not updating. Could you please check the Info message.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-29-2023 08:41 AM
Hi @Venkat141,
try to use update operation,
current.update() in Business rule.
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 08:47 AM
Hello,
Now it's working. Could you please explain the code
- 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.