Setting a Date-Time field with a GMT value

Jamsta1912
Tera Guru

Hello all,

I'm trying to write a business rule that manipulates a date/time string always provided to us in GMT, to then populate a date-time field on a record.

I'm struggling to populate the field with the GMT time, ie it's always interpreted as the display time (local time).

Is there a function that directly sets a datetime field with the GMT time, so that the display time is then in the correct time zone?

I've tried using setValueUTC:

var dtNew = new GlideDateTime();

dtNew.setValueUTC("15-02-2011 08:00:00", "dd-MM-yyyy HH:mm:ss");

my_record.my_date_time = dtNew;

but in this case it's the display time of 'my_date_time' that ends up set to 15-02-2011 08:00:00.

5 REPLIES 5

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Thank you Pradeep. I am not sure how to make use of this...


If I have a GlideDateTime object, how do I set it to GMT using setTZ(tz), ie what do I need to pass in as tz?



Jamsta1912
Tera Guru

I've done some more playing around, setting tz to some different values:



var tz = Packages.java.util.TimeZone.getTimeZone("Canada/Eastern");


dtNew.setTZ(tz);



But


my_record.my_date_time = dtNew;


always sets the field with the same display value.



I feel I'm missing something fundamental!



Just to reiterate what I'm trying to do. I have a date-time, eg '2015-08-20 12:00:00'.


This date-time is GMT, and will be all year round. I want to set the value of a date-time field on a record to match this. We're GMT + 1 locally at the moment so when I set the field, it should display as 2015-08-20 13:00:00. Later in the year, when we're back on GMT locally, if I have date '2015-12-20 12:00:00' it should also display as '2015-12-20 12:00:00'.



Any help, appreciated.


Right, sussed it!


I just needed to use setValue().   That sets the GMT value of the GlideDateTime object.