How to populate time type field (not date)

Eli Guttman
Tera Guru

Hi,

I have a table field of time type (not date).

i have an input value from text variable that indicate the time, for example: 09:00 / 11:00 / 15:00.

How can i (using a script ) populate the time field?

What is the correct syntax to populate a time field?

Thank you!

1 ACCEPTED SOLUTION

Geoffrey2
ServiceNow Employee
ServiceNow Employee

find_real_file.png

find_real_file.png

It's not as simple as you might hope. A Time field is stored as a Date/Time value. The date is always 1st Jan 1970. And there is time zone conversion to deal with.

For example, I entered a time of 5:34 AM, but the stored value is 1970-01-01 13:34:00 (1:34 PM).

To update the field with a script, you need to use something like this:

var time = '05:00';

var gdt = new GlideDateTime('1970-01-01 ' + time + ':00');
gdt.addSeconds(gdt.getTZOffset()/-1000)

current.u_time = gdt;

View solution in original post

3 REPLIES 3

Chalan B L
Giga Guru

Hello Elig,

 

var dt = new GlideDateTime(current.start_date);

var datetimearray = dt.getDisplayValue().split(' ');

var date = datetimearray[0];
var time = datetimearray[1];

 

----**********

Checked the code in background script -- worked fine

*** Script: 14:07:24
*** Script: 2018-06-08

----***********

 

Please have a look at the below thread

https://community.servicenow.com/community?id=community_question&sys_id=64a60365db1cdbc01dcaf3231f9619d9

 

Mark the answer as correct and helpful if it helped you..!!

 

Regards,

Chalan

Geoffrey2
ServiceNow Employee
ServiceNow Employee

find_real_file.png

find_real_file.png

It's not as simple as you might hope. A Time field is stored as a Date/Time value. The date is always 1st Jan 1970. And there is time zone conversion to deal with.

For example, I entered a time of 5:34 AM, but the stored value is 1970-01-01 13:34:00 (1:34 PM).

To update the field with a script, you need to use something like this:

var time = '05:00';

var gdt = new GlideDateTime('1970-01-01 ' + time + ':00');
gdt.addSeconds(gdt.getTZOffset()/-1000)

current.u_time = gdt;

Pranay Tiwari
Kilo Guru

Hi,

Try below code it will populate time as per your requirement..,

 

	var gr = new GlideRecord("u_xml_data");//pass table name
gr.addQuery("sys_id", "609df9004fa1a3007c750f5e9310c7a5");//write your query
gr.query();
if (gr.next()) {
  gs.addInfoMessage(gr.getDisplayValue("u_time"));//pass time field name
}

 

Mark correct or helpful if it helps you.

 

Warm Regards,

Pranay Tiwari

| www.DxSherpa.com | pranay.tiwari@dxsherpa.com |