Data and Time Values not getting Exactly

Lucky1
Tera Guru

Hello all,

 

I am getting a change request's "Planned Start date" and "Planned End Date" values in a scripts background.

But I see there is a difference in the time shown for the data of those fields on the form and to my scripts background.

 

On the change request form:

Planned Start date: 03-11-2024 05:30:00

Planned End date: 03-11-2024 09:30:00

 

But in the scripts background it has given as:

Start date is: 2024-11-03 00:00:00

End date is: 2024-11-03 04:00:00

 

 

So, what should I do to get the exact date and time format and also exact values as shown on the form?

Please help.

 

 

Regards,

Lucky

1 ACCEPTED SOLUTION

Robert H
Mega Sage

Hello @Lucky1 ,

 

ServiceNow uses UTC as the time zone, and the YYYY-MM-DD format when storing dates on records. 

That's what you get in your script when you access the raw values.

What you see in the UI is based on your own time zone and the date/time format you selected in your preferences.

If you want your script to print the date in the same time zone and format you can do that using "getDisplayValue":

 

gs.info('raw value: ' + gr.start_date);
gs.info('display value: ' + gr.getDisplayValue('start_date'));

 

Output:

 

raw value: 2025-05-24 12:30:00
display value: 24/05/2025 05:30:00

 

Regards,

Robert

View solution in original post

6 REPLIES 6

Robert H
Mega Sage

Hello @Lucky1 ,

 

ServiceNow uses UTC as the time zone, and the YYYY-MM-DD format when storing dates on records. 

That's what you get in your script when you access the raw values.

What you see in the UI is based on your own time zone and the date/time format you selected in your preferences.

If you want your script to print the date in the same time zone and format you can do that using "getDisplayValue":

 

gs.info('raw value: ' + gr.start_date);
gs.info('display value: ' + gr.getDisplayValue('start_date'));

 

Output:

 

raw value: 2025-05-24 12:30:00
display value: 24/05/2025 05:30:00

 

Regards,

Robert

Thank you Robert,

 

It's working.

 

 

Regards,

Lucky

Daniel Madsen
Kilo Sage

Hi Lucky1

 

I think it is related to the change request form showing the time in the user's time zone, whereas the background script is showing the servers time zone that was saved on the record. You can verify by running this background script:

 

var changeRequest = new GlideRecord('change_request');
if (changeRequest.get('INSERT-CHANGE-REQUEST-SYSID-HERE')) {

	var user = gs.getUser();
	var timezone = user.getTZ();

  	var plannedStart = new GlideDateTime(changeRequest.start_date);
  	var plannedEnd = new GlideDateTime(changeRequest.end_date);

	gs.info("Server Planned Start: " + changeRequest.start_date);
 	 gs.info("Server Planned End: " + changeRequest.end_date);

	plannedStart.setTimeZone(timezone);
	plannedEnd.setTimeZone(timezone);
  
  gs.info("User Planned Start: " + plannedStart.getDisplayValue());
  gs.info("User Planned End: " + plannedEnd.getDisplayValue());
}

 

If this helps, please give it a helpful vote. And if it’s what you were looking for, go ahead and accept the solution. Thanks,
Daniel Madsen

J Siva
Tera Sage

Hi @Lucky1 

I believe you're using getValue() in your background script.

By default, ServiceNow stores date/time values in UTC within the database.

Use getDisplayValue() to retrieve the date/time in the currently logged-in user's time zone.

Regards,

Siva