- 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-21-2016 04:09 PM
Hi Nate,
Seeing that you're only calculating the difference between the date, have you tried without the getDisplayValue() ? i.e. gs.dateDiff(current.u_start_date,current.u_end_date);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-21-2016 06:32 PM
namnguyen I just tried this and received the same results. I am thinking about parsing out the am/pm and adding 00 seconds on the end and just see what the client says
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-21-2016 07:27 PM
awesome, how did it work out if you take out the am/pm and add the seconds?
just out of curiosity, any chance the u_start_date and u_end_date columns are of string type instead of date/time?
another thing I can think of would be parsing the field value into a GlideDateTime variable, something like this:
var start_date = new GlideDateTime();
var end_date = new GlideDateTime();
start_date.setDisplayValue(current.u_start_date.getDisplayValue());
end_date.setDisplayValue(current.u_end_date.getDisplayValue());
current.u_duration = gs.dateDiff(start_date.getValue(),end_date.getValue());
gs.log("start date: " + start_date.getDisplayValue());
gs.log("end date : " + end_date.getDisplayValue());
gs.log("True Duration is : " + gs.dateDiff(start_date.getValue(),end_date.getValue(),true));
gs.log("False Duration is : " + gs.dateDiff(start_date.getValue(),end_date.getValue(),false));
gs.log("This Duration is : " + gs.dateDiff(start_date.getValue(),end_date.getValue()));
- 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