- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-21-2016 02:40 PM
Hello All,
I am running into an issue with a current clients instance. They have their time format under the system properties as "hh:mm a" so all date fields are in this format "09-21-2016 03:43 PM" I am trying to set the value of a duration field using a start and end date but the issue is when I use gs.dateDiff(current.u_start_date.getDisplayValue(),current.u_end_date.getDisplayValue()); with the third parameter true/false or blank I get invalid results. because the display value excludes the seconds and has the am/pm.
Is there a way around this? Does Sn still store the seconds even though the format is excluding it.
I did some digging into the metric table and it is running this code and the start and end dates are the same but it is working, but not for my business rule.
This oob metric definition "Create to Resolve Duration" is running this script:(notice the am/pm and the duration is calculating)
my script:
(function executeRule(current, previous /*null when async*/) {
// Add your code here
//setting the duration field to ms
//get date difference between start and end date
getTimeDiff();
function getTimeDiff(){
var startDate = newDateTime();
var ednDate = newDateTime();
current.u_duration = gs.dateDiff(current.u_start_date.getDisplayValue(),current.u_end_date.getDisplayValue());
gs.log("start date: " + current.u_start_date);
gs.log("end date : " + current.u_end_date);
gs.log("True Duration is : " + gs.dateDiff(current.u_start_date.getDisplayValue(),current.u_end_date.getDisplayValue(),true));
gs.log("False Duration is : " + gs.dateDiff(current.u_start_date.getDisplayValue(),current.u_end_date.getDisplayValue(),false));
gs.log("This Duration is : " + gs.dateDiff(current.u_start_date.getDisplayValue(),current.u_end_date.getDisplayValue()));
}
})(current, previous);
Thanks for helping,
Nate
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-22-2016 08:14 AM
namnguyen I ended up doing it this way:
getTimeDiff();
function getTimeDiff(){
var startDate = current.u_start_date.getGlideObject();
var endDate = current.u_end_date.getGlideObject();
current.u_duration = gs.dateDiff(startDate.getDisplayValueInternal(),endDate.getDisplayValueInternal(),false);
}
It still does not get me the seconds, which I guess has to do with the time format. I think this will do though
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-22-2016 01:10 PM
Awesome this has got to be the cleanest
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-30-2016 03:05 PM
This ended up being the end result. I had to base the duration also off of the Schedule on the SLA
getTimeDiff();
function getTimeDiff(){
var dur = calcDurationSchedule(current.u_start_date,current.u_end_date);
current.u_duration = dur;
function calcDurationSchedule(start, end) {
var sla = current.u_task_sla.schedule;
if( sla != '' ){
var sched = new GlideSchedule(sla);
return (sched.duration(start.getGlideObject(), end.getGlideObject()));
}else{
var startDate = current.u_start_date.getGlideObject();
var endDate = current.u_end_date.getGlideObject();
return gs.dateDiff(startDate.getDisplayValueInternal(),endDate.getDisplayValueInternal(),false);
}
}
}