Convert GlideDateTime AM/PM to 24-hour format for calculating duration

Jonathan77
Kilo Contributor

Hi, I am trying to get the Duration based on the difference between two GlideDateTimes. Getting the display value from the GlideDateTime does not seem to be pulling AM/PM or converting it to a 24-hour format for my calculation. My script is below. Is there something I am missing from this script to get the GlideDateTime in 24-hour format? 

 

Thanks in advance

 

(function executeRule(current, previous /*null when async*/) {

           

var actualstart = new GlideDateTime(current.u_diagnosis_time.getDisplayValue());

var actualfinish = new GlideDateTime(current.u_repair_time.getDisplayValue());

 

//If the begin and end times are present, calculate the duration

if (actualstart && actualfinish) {

 //  var difference = gs.dateDiff(actualstart, actualfinish, false);

            var difference = GlideDateTime.subtract(actualstart, actualfinish); 


   current.u_dttrt.setValue(difference); 
}

 

})(current, previous);

 

1 ACCEPTED SOLUTION

Brad Bowman
Kilo Patron
Kilo Patron

Date/Time fields are stored in 24-hour format, it's the display preference that can make it am/pm, so I would just use:

var actualstart = new GlideDateTime(current.u_diagnosis_time);

var actualfinish = new GlideDateTime(current.u_repair_time);

View solution in original post

5 REPLIES 5

Brad Bowman
Kilo Patron
Kilo Patron

Date/Time fields are stored in 24-hour format, it's the display preference that can make it am/pm, so I would just use:

var actualstart = new GlideDateTime(current.u_diagnosis_time);

var actualfinish = new GlideDateTime(current.u_repair_time);

Thanks for the help Brad that solved my issue. The getDisplayValue() was unnecessary 

This is not working for me.

 

var start = "2021-06-28 08:00:00 PM";
var startdate = new GlideDateTime(start);
gs.print(startdate);

I get this:

*** Script: 2021-06-28 08:00:00

I figured it out... 

***

var start = "2021-06-28 08:00:00 PM";
var startdate = new GlideDateTime();
startdate.setDisplayValue(start, 'yyyy-mm-dd hh:mm:ss');
gs.print(startdate);