Insert UTC Time into A Date/Time Field Using GlideRecord

kiki330611
Kilo Expert

Hi Guys,

We are trying to use gliderecord to create a new record in a table.

When writing the date/time, we noticed that it's written in as the display time (local time) not the actual time (UTC).

Example like below:

---

var gr = new GlideRecord('u_table');

gr.initialize();

gr.start_date = "03/06/2017 22:10:40"

gr.insert();

---

The script above will write into the start date field with the display value of 03/06/2017 22:10:40.

However, is there a way to insert the UTC time instead?

Thanks!

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Kiki,



ServiceNow stores the date/time field value in UTC only.


Also is the field "start_date" in "u_table" table a date/time field?



Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.


Thanks


Ankur


Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Hi Ankur,



Yes I know it stores UTC as the actual value but it display differently in the instance according to the user's local time.


My problem was the time I inserted into the field would become "local time" instead of UTC time and I am hoping to be able to enter UTC time.


So if I put gr.start_date = A, A shows up in our instance as the local time.


What I would like to do is gr.start_date = A and it shows up as B (local time) and A is the actual system time.


The start_date field is a date/time field.


Doesn't answer op's question. I'll post a separate answer.

Chris Nack
Tera Expert

Understanding that ServiceNow Date/Time columns are always stored as UTC, I did an experiment where I tried assigning a value to a Date/Time column two different ways:

 

1. gr = new GlideRecord('u_demo');
2. gdt = new GlideDateTime("2019 09 16 00:00:00");
3. gr['u_date_stamp'] = gdt;
4. gr.insert();


5. gr = new GlideRecord('u_demo');
6. gr['u_date_stamp'] = "2019 09 16 00:00:00";
7. gr.insert();
 
 
For the first record inserted, I set the column value directly to a GlideDateTime instance as shown on line 3. The GlideDateTime constructor used on line 2 assumes UTC time zone. The value inserted into the table was not changed...it was "2019-09-16 00:00:00" in the internal representation. The string I passed to the GlideDateTime constructor was interpreted in UTC time zone, and the same value was written into the table unchanged.
 
In the second record inserted, I set the date time field to a string containing  the date/timestamp as shown on line 6. In this case, ServiceNow interpreted the value relative to my user's time zone (which is currently Eastern US). The actual value written to the table was 4 hours later (which is the current offset between Eastern US and UTC time zones).
 
So the bottom line is if you want to write a UTC date/time into the table, assign the column value directly to a GlideDateTime instance as shown on line 3. Don't use the string assignment as shown on line 6.