"gr.getValue()" and "gr.getByFormat('hh:mm:ss')" return different times

PB7
Mega Guru
Hi Team,
 
I am drafting the below script to extract the HH:MM:SS from a date/time field for mapping to other fields ---
However, I'm seeing a 12-hour discrepancy. Please observe the attached snip [PFA-1].
Can anyone help me understand why there are two different times returned by '.getValue' and '.getByFormat' respectively? This is the target field[PFA-2].
 
 
var ct = new GlideRecord('u_coverage_tickets');
 
ct.addQuery('sys_id','004f3a4587c34154eaf041d8cebb3546'); //COT0001758
ct.query();
gs.log("CT-test " + ct.getRowCount());
 
if (ct.next()){
 
var gdt = new GlideDateTime(ct.u_previous_task_date);
 
gt = gdt.getTime();
gs.info("  value is " + gt.getValue()); //  *** Script: value is 1970-01-01 17:00:00
gs.info("  time is " + gt.getByFormat('hh:mm:ss')); // *** Script:   time is 05:00:00
}
6 REPLIES 6

Isaac Vicentini
Mega Sage
Mega Sage

The discrepancy you're observing between the outputs of getValue and getByFormat methods in your script appears to be related to time zone handling.

 

  1. getValue Method: This method returns the date and time in UTC (Coordinated Universal Time). Your output of 1970-01-01 17:00:00 is in UTC.

  2. getByFormat Method: This method returns the time formatted according to the specified format, in this case, 'hh:mm:ss'. However, it seems to be returning the time in your local time zone or the time zone configured in your ServiceNow instance, which appears to be 12 hours behind UTC. Hence, the output is 05:00:00.

 

GlideDateTime and GlideTime classes handle time zones, and it's essential to be mindful of this when working with date and time in ServiceNow. The GlideDateTime object (gdt in your script) represents a specific moment in time, and the time zone can impact how that moment is represented.

 

To resolve this discrepancy, you have a couple of options:

  • Standardize Time Zones: Ensure that all your date-time manipulations and representations are in a single time zone (either UTC or your local time zone). You can use methods like setTZ to explicitly set the time zone.

  • Adjust the Format Method: If you want to keep using local time, adjust the getByFormat method to ensure it's clear which time zone you are displaying.

 

If you need the time in a specific time zone, you should explicitly set or convert the time zone before retrieving the time. ServiceNow offers methods to handle time zone conversions which can be used to ensure consistency in your script's output.


Best regards,

Isaac Vicentini
MVP 2025


If my answer was helpful, mark it as Helpful or Accept as Solution.

Shubham Singh
Mega Guru

Hi @PB7 

 

Your field is not returning value in string so it is taking old 1970s date randomly in GlideRecord.

 

As your gdt variable stores the value 1970-01-01 17:00:00.

  • gdt.getValue() returns the same time 17:00:00.
  • gdt.grtByFormat(‘hh:mm:ss’) works with 12 hrs am pm cycle. So it is returning 05:00:00.
  • gdt.getByFormat(‘HH:MM:SS’) works 24 hrs cycle. It will return 17:00:00 only.

Now to get the proper date always use below format:

 

var gdt = new GlideDateTime();  //if we pass date here, it will convert automatically to system timezone

gdt.setDisplayValue(ct.u_previous_task_date.toString());

gs.addInfoMessage(gdt.getTime());

 

Thanks!

 

Please mark this response as correct and helpful ✔️👍

Hi @PB7 

 

Please mark this answer as correct if it is works for you!

 

Regards,

Shubham Singh

I will do so ; still trying to get it to work.  I'm not seeing any output when I run the bottom script.

I get this to the console:

 

Run Fix Script

*** Script: CT-test 1
*** Script: Coverage Ticket template number is COT0001758
[0:00:00.017] Total Time

 

-- this is the FixScript I'm running:

 

var ct = new GlideRecord('u_coverage_tickets');
 
ct.addQuery('sys_id','004f3a4587c34154eaf041d8cebb3546'); //COT0001758
ct.query();
gs.log("CT-test " + ct.getRowCount());
if (ct.next()){
gs.print("Coverage Ticket template number is " + ct.getValue('u_number'));
var gdt = new GlideDateTime();

gdt.setDisplayValue(ct.u_previous_task_date.toString());

gs.addInfoMessage(gdt.getTime());

}

 

 

 

+++

 

Any thoughts?