Date time issue

arpitha
Tera Expert

I am trying to set the date value like this, but when I set this value in the field, it is set as  '2024-04-03 03:00:00'

4 hours before value,I tied most dates and time logic, but nothing works can someone please help with the same

 

value = '2024-04-03 07:00:00';

3 REPLIES 3

Manikmodi16
Tera Guru

Hi,

 

I tried to update a data/time field using GlideRecord and it updated as per the entered value. Can you try with the below code:

 

var gr = new GlideRecord('incident');
gr.addQuery('number','INC0009009');
gr.query();

while(gr.next()){
    gr.u_test_date_time = '2024-01-29 01:35:00'; //Setting value for the date/time field
    gr.update();
}

 

Manikmodi16_0-1706515611000.png

 

Please mark this reply as the solution if you find it helpful. Thanks!

Robbie
Kilo Patron
Kilo Patron

Hi @arpitha,

 

Working with dates/times is always fun.

Whilst I can absolutely help you set the date/time as desired, I first want to set some context and explain some date/time fundamentals to ensure you set the correct values.

 

It's important to remember (and know for good reason), that all times are stored in Coordinated Universal Time (UTC) and appear globally based on the system time zone. 

Importantly, however, times appear to users in their local timezone, according to their user preference settings.

This is why you see a difference between the value you are setting and what is displayed. I believe you are setting the value using the default UTC value and your user preference has a timezone which factors in the difference.

This is expected behavior. 

It is highly recommended unless you are absolutely sure and comfortable working with timezones, you should always set the time in UTC.

 

If you step back and think about users globally, the time now based in your time zone will always be different for another user who is based in another timezone.

For example, if the date/time in the UK (GMT) is 2024-01-29 11:10:48, the time in the US East Coast (ET) is 2024-01-29 06:10:48 (-5 hours including daylight savings time). The 'Opened at' time should reflect the same relative time for everyone. 

 

I would recommend you play around with the below script in your Dev environment in order to understand a few important factors when setting the time.

Remember, it is highly recommended the time is set UTC. 

 

Firstly, understand your instance timezone. This is easily found either under:

  1. System Properties > Basic Configuration
  2. Scroll to the System timezone for all users unless overridden in the user's record heading

     Or navigate to the sys_property: glide.sys.default.tz

     (By default, the field is blank. If you have not defined a time zone for this property, America/Los Angeles is the default.)

 

Here's a script that you can play with (reminder in Dev or your PDI) to understand the different values with dates, as well as an example of how to set the date in a chosen timezone:

 

// Example timezone values
var timeNow = new GlideDateTime();
gs.print("Time Now (Always UTC): " + timeNow);
var timeNowUSETimezone = timeNow;
timeNowUSETimezone.setTimeZone('US/Eastern');
gs.print("Time Now on your instance in chosen TZ (US/Eastern): " + timeNowUSETimezone.getDisplayValue());
gs.print('Instance timeZone Setting (If Blank, America/Los Angeles): ' + gs.getProperty('glide.sys.default.tz'));
var instanceDateTime = timeNow;
instanceDateTime.setTimeZone('America/Los_Angeles');
gs.print("America/Los Angeles: " + instanceDateTime.getDisplayValue());

//Example of how to set a date/time field with a specific timezone
var incGR = new GlideRecord('incident');
incGR.addQuery('sys_id', 'a83820b58f723300e7e16c7827bdeed2'); //Use a sys_id of an incident record
incGR.query();
if(incGR.next()){
   gs.print('Inc no: ' + incGR.number);
   var tmpTime = new GlideDateTime();
   tmpTime.setDisplayValue(incGR.getDisplayValue('opened_at'));
   gs.print("UTC value of opened_at field: " + tmpTime);
   tmpTime.setTimeZone('US/Eastern');
   gs.print("US/Eastern value of opened_at field: " + tmpTime.getDisplayValue());
   //Uncomment the below 2 lines to update the opened_at field in US/Eastern
   //incGR.opened_at = timeNowUSETimezone.getDisplayValue();
   //incGR.update();
}
 
Please also review the GlideDate API found here with some good examples and usage: https://developer.servicenow.com/dev.do#!/reference/api/tokyo/server/c_APIRef
 
 

To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Helpful.

 

Thanks, Robbie

Robbie
Kilo Patron
Kilo Patron

Hi @arpitha,

 

Did you see my earlier response where I provided a sample script to set the time in a certain timezone?

Please take a look at my response and as I recommended, play around in your Dev or PDI environment with the timezone script functions.

 

@arpitha - To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Helpful.

 

Thanks, Robbie