setDisplayValue() in glideDateTime

ServiceNow Use6
Tera Guru

Hi,

I am unable to understand how we get this output. Kindly help.

var gdt = new GlideDateTime("2011-02-02 12:00:00");
gdt.setDisplayValue("2011-01-01 12:00:00");
gs.print(gdt.getValue());



2011-01-01 20:00:00

 

Regards

Suman P.

 

3 REPLIES 3

Masthan Sharif
Tera Guru

Hi @ServiceNow Use6 Suman,

Hope you're doing well!

setDisplayValue() interprets the string in the user's time zone—your local time. Internally, GlideDateTime stores UTC values, when you print with getValue(), you're seeing the UTC equivalent, which shifts the time based on your time zone offset. Your timezone should be UTC-8, right?

getValue(): Returns the UTC-stored datetime in database format
getDisplayValue(): Returns the datetime translated into the user's time zone and format.

var gdt = new GlideDateTime();                       
gdt.setDisplayValue("2011-01-01 12:00:00");          
gs.print("getValue → " + gdt.getValue());           // e.g. "2011-01-01 20:00:00" UTC due to timezone
gs.print("getDisplayValue → " + gdt.getDisplayValue()); // "2011-01-01 12:00:00" local time

 

Best Regards,

Sharif

Santosh Oraon
Tera Expert

Hi @ServiceNow Use6  Its simple the setDisplayValue() - set your time in local timzone and gdt.getValue() always gives the time in GMT.

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Santosh Oraon

Mohammad Danis3
Tera Expert

Hi @ServiceNow Use6 ,

  1. Input Format: The setDisplayValue() method expects the date-time string in the format yyyy-MM-dd HH:mm:ss.
  2. Time Zone: The display value is interpreted in the user's time zone.

This method is particularly useful when you need to manipulate or set date-time values programmatically in ServiceNow scripts.
getValue(): Returns the value of the field as a string in UTC format.

Please close the thread by marking answer correct so that it benefits future readers.

Regards,
Mohammad Danish