- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-12-2018 11:12 PM
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!
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-13-2018 12:16 AM
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;

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-12-2018 11:27 PM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-13-2018 12:16 AM
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;

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-13-2018 01:42 AM
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 |