Why setDisplayValue in glide date time object adding 6 hrs?

Manikantahere
Tera Contributor
var startdate =  2024-11-13
var gdtStartDate = new GlideDateTime();
  gs.print(gdtStartDate);
 gdtStartDate.setDisplayValue( startdate + ' 00:00:00');
 
Why it is printing date with 6 hrs.? as it is supposed to print with 2024-11-13 00:00:00?
2024-11-13 06:00:00
7 REPLIES 7

I am not creating any script I want to know about existed script where they used setDisplayValue.

 

what makes difference between getValue and getDisplayValue ?

 

why its been showing as I shown above when i used getDisplayValue is the thing I didn't understand?

Neeraj_27
Tera Guru

@Manikantahere ,

Basically getDisplayValue return in user's format and timezone. So thats the reason behind adding 6 hrs.

NeerajMadishet_0-1730809705809.png

 

Simon Christens
Kilo Sage

Its because your timezone is UTC-6 I would say.

When you use setDisplayValue() - depending on you own timezone - the actual value (setValue) changes to UTC+0
In my case because im in UTC+1 when i do the following my GDT will change the date to "yesterday at 23:00"
This is because I set the displayValue to midnight which actually tell ServiceNow to use 2024-22-04 23:00:00 as UTC date / time

 

var gdt = new GlideDateTime();
gdt.setDisplayValue('2024-11-05 00:00:00');
gs.info(gdt);

 

*** Script: 2024-11-04 23:00:00

When you set DisplayValue - you dont want to reteive gdt or .getValue() - because thats always converted either forward or backward depending on your own timezone. 
Either SET DisplayValue AND get DisplayValue

Or SET Value AND get Value

Else you will confuse yourself

 

var gdt = new GlideDateTime();
gdt.setDisplayValue('2024-11-05 00:00:00');

gs.info(gdt.getDisplayValue());

 

Gives you

*** Script: 05/11/2024 00:00:00