- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-16-2021 05:58 AM
Hello Expert,
I want to calculate the number of days between two date fields. but this give me the less number of days between the two dates.
For example, the dates are 17-05-2021 to 18-05-2021, I am using the script below, the script is returning the value of '1', whereas it should be '2'. How can I get the correct value?
function setNumberOfDays() {
var start = current.u_start_date;
var end = current.u_end_date;
var d = gs.dateDiff(date1, date2, true);
current.u_total_number_of_days = d;
}
Thanks in advanced.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-16-2021 06:02 AM
Hi Dhanshri,
You will want to convert the dates to the beginning and end of the days in some fashion.
So, Please refer to the below script code it will help you...
function setNumberOfDays() {
var start = new GlideDateTime('current.u_start_date');
var end = new GlideDateTime('current.u_end_date');
var diff = GlideDateTime.subtract(start, end);
var days = diff.getRoundedDayPart();
current.u_total_number_of_days = days;
}
Else try like this in Background script:
var start = new GlideDateTime('2021-05-17 00:00:00');
var end = new GlideDateTime('2021-05-18 23:59:59');
var diff = GlideDateTime.subtract(start, end);
var days = diff.getRoundedDayPart();
gs.print(start);
gs.print(end);
gs.print(days);
output:
*** Script: 2021-05-17 00:00:00
*** Script: 2017-05-18 23:59:59
*** Script: 2
For more details: GlideDateTime.subtract() and GlideDuration.getRoundedDayPart()
Mark the answer as ✅ Correct/Helpful based on its impact.
Thanks & Regards,
Tejas Tamboli
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-16-2021 06:34 AM
Just replace your code with this code :
var start = new GlideDateTime(current.getValue('u_start_date'));
var end = new GlideDateTime(current.getValue('u_end_date'));
var dur = new GlideDuration();
dur = GlideDateTime.subtract(start, end);
var day = dur.getDayPart();
var calculation = day + 1;
current.u_total_number_of_days = calculation;
Regards
Prasad
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-10-2023 10:04 AM
var METRICDEF = 'be28f1791b98251021505354604bcba9'; //MTRC0010026
var approvalGr = new GlideRecord('sysapproval_approver');
//approvalGr.addEncodedQuery('source_table=sc_req_item^state=approved^sys_created_on<javascript:gs.beginningOfThisMonth()'); //Getting approved RITM's which are created before this month
approvalGr.addEncodedQuery('source_table=sc_req_item^state=approved^sys_created_onONLast 6 months@javascript:gs.beginningOfLast6Months()@javascript:gs.endOfLast6Months()');
approvalGr.query();
while(approvalGr.next()){
var gdtStart = new GlideDateTime(approvalGr.getDisplayValue('sys_created_on'));
gdtStart.getByFormat("dd-MMM-yyyy HH:MM:SS");
var gdtEnd = new GlideDateTime(approvalGr.getDisplayValue('sys_updated_on'));
gdtEnd.getByFormat("dd-MMM-yyyy HH:MM:SS");
var dur = new GlideDuration();
var metricInstanceGr = new GlideRecord('metric_instance');
metricInstanceGr.initialize();
metricInstanceGr.setValue('definition',METRICDEF);
metricInstanceGr.setValue('table','sysapproval_approver');
metricInstanceGr.setValue('field','state');
metricInstanceGr.setValue('id',approvalGr.getUniqueValue());
metricInstanceGr.setValue('start',gdtStart.getDisplayValue());
metricInstanceGr.setValue('end',gdtEnd.getDisplayValue());
metricInstanceGr.setValue('calculation_complete',true);
metricInstanceGr.setValue('value','Requested');
metricInstanceGr.setValue('dur',GlideDateTime.subtract(gdtStart,gdtEnd));
metricInstanceGr.insert();
}
I am using the above script for calculating the duration of approvals
but i am getting values like this
how duration is calculating
could you please help to correct my script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-16-2021 06:37 AM
and if you wish to use same script then :
function setNumberOfDays() {
var start = current.u_start_date;
var end = current.u_end_date;
var d = gs.dateDiff(date1, date2, true);
var calculation = d + 1;
current.u_total_number_of_days = calculation ;
}
Script should be like this.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-16-2021 06:52 AM
Hi
Try this
Math.floor(gs.dateDiff(start_date,end_date,true)/86400);
Please mark my answer as helpful/correct, if applicable.
Best regards,
Sai Kumar