Issue in GlideDate format in scoped application
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-31-2022 02:26 AM
Hi @Ankur Bawiskar ,
We have a requirement where we need to calculate the difference between two date/time fields and the answer should populate in another duration field
Here, we are using client script and script include
In client script we are getting the values of the date/time values as (dd/mm/yy hh:mm:ss) eg:start date: 29/06/2022 13:24:12, end date: 31/10/2022 13:24:12 format but when in script include it is taking as (yyyy/mm/dd 00:00:00) eg: 2022-06-29 00:00:00, 2022-10-31 00:00:00
But when i tried to change the format of start date on form as " 06/29/2022 13:24:12" then it is taking as "2022-06-29 13:24:12" and populating the exact duration value in duration field
Note: We are using GlideDate time in script include and we are calculating the difference of start date and end date as "GlideDateTime.Subtract(start date, end date)"
- Labels:
-
Incident Management
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-31-2022 02:39 AM
you are correct on using GlideDateTime.subtract() method.
It's just that formatting needs to be corrected.
the date/time value you are passing to script include must be in logged in user's format as different users will have different date formats.
Ensure you set the correct value when you set the date/time in GlideDateTime object.
Can you share your script here and not screenshots?
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-31-2022 02:44 AM
Thank you for quick reply.
can you please tell us how can we format date and time. we are not find the solution to format the date and time.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-31-2022 02:58 AM
Since the question is asked by @sasikanth1 it would be better if the person shares/responds so that the person can mark appropriate response as correct
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-31-2022 02:55 AM - edited 10-31-2022 02:56 AM
Thanks for quick around ankur, Please find the below client script and script include
we are using the fields strt_date/en _date using "glide_date_time" type and duration field type is "glide_duration"
client script:
var strt = g_form.getValue('strt_date');
var end = g_form.getValue('en_date');
if(strt != ' ' &&end !=' ' && newValue != ' '){
var ajDur = new GlideAjax('scriptinclude');
ajDur.addParam('sysparm_name', 'cal');
ajDur.addParam('sysparm_start', strt');
ajDur.addParam('sysparm_end', end);
ajDur.getXMLAnswer(function(answer)){
g_form.setValue('duration', answer);
});
}else {
g_form.setValue('duration', ' ');
}
}
Script include:
cal : function(){
var startDate = GlideDateTime(this.getParameter('sysparm_start'));
var endDate = GlideDateTime(this.getParameter('sysparm_end'));
var dur = GlideDateTime.subtract(startDate, endDate);
return(dur);
},