Get the local date and time with GlideDateTime

Waffle
Tera Expert

Hello,

 

I want to get the sys_created_on and sys_updated_on date of a catalog task in the fix script. But the dates I got is different than what is display in the catalog task.

 

I am suspecting the sys_created_on stores the UTC time, so I tried using GlideDateTime to set the sys_created_on to my time zone (Canada Mountain Time), which should -6 hours from the UTC time, but it actually added 6 hours. Am I missing something in my code?

var grTask = new GlideRecord('sc_task');
grTask.get('<catalog task SYS ID>');

if (grTask) {
	gs.print('Number: ' + grTask.number);
	gs.print('Created on: ' + grTask.sys_created_on);
	gs.print('Updated on: ' + grTask.sys_updated_on);

	// Output:
	// Created on: 2024-10-15 06:09:01
	// Updated on: 2024-10-15 13:05:03

	// Created on in the Catalog Task showing 2024-10-15 00:09:01
	// Updated on in the Catalog Task showing 2024-10-15 07:05:03

	var createdGr = new GlideDateTime();
	createdGr.setDisplayValue(grTask.sys_created_on);

	gs.print(createdGr.getValue());

	// Output:
	// 2024-10-15 12:09:01
}

 Thanks,

1 ACCEPTED SOLUTION

Astik Thombare
Tera Sage

Hi @Waffle ,

 


The issue you’re facing is due to the time zone difference between the display value (what the user sees, adjusted to their local time zone) and the system value (which is stored in UTC). In your script, you’re setting the display value (sys_created_on) directly to the GlideDateTime object, which may not work as expected because the setDisplayValue method expects a string formatted according to the user’s time zone (not UTC).

 

Here’s how you can correctly handle the conversion between UTC and your local time zone (Canada Mountain Time):

 

Solution:


Retrieve the system value (sys_created_on and sys_updated_on) in UTC.


Convert the value to your local time zone using getDisplayValue() instead of setDisplayValue().

 

var grTask = new GlideRecord('sc_task');
grTask.get('<catalog task SYS ID>');

if (grTask) {
    gs.print('Number: ' + grTask.number);
    gs.print('Created on (UTC): ' + grTask.sys_created_on);
    gs.print('Updated on (UTC): ' + grTask.sys_updated_on);

    // Convert sys_created_on and sys_updated_on to the current user's time zone (Canada Mountain Time)
    var createdDate = new GlideDateTime(grTask.sys_created_on);
    var updatedDate = new GlideDateTime(grTask.sys_updated_on);

    gs.print('Created on (Local): ' + createdDate.getDisplayValue()); // Shows time in the user’s time zone
    gs.print('Updated on (Local): ' + updatedDate.getDisplayValue()); // Shows time in the user’s time zone
}

 

If my reply helped with your issue please mark helpful 👍and correct ✔️if your issue is resolved.

 

By doing so you help other community members find resolved questions which may relate to an issue they're having

 

Thanks,

Astik

View solution in original post

2 REPLIES 2

Astik Thombare
Tera Sage

Hi @Waffle ,

 


The issue you’re facing is due to the time zone difference between the display value (what the user sees, adjusted to their local time zone) and the system value (which is stored in UTC). In your script, you’re setting the display value (sys_created_on) directly to the GlideDateTime object, which may not work as expected because the setDisplayValue method expects a string formatted according to the user’s time zone (not UTC).

 

Here’s how you can correctly handle the conversion between UTC and your local time zone (Canada Mountain Time):

 

Solution:


Retrieve the system value (sys_created_on and sys_updated_on) in UTC.


Convert the value to your local time zone using getDisplayValue() instead of setDisplayValue().

 

var grTask = new GlideRecord('sc_task');
grTask.get('<catalog task SYS ID>');

if (grTask) {
    gs.print('Number: ' + grTask.number);
    gs.print('Created on (UTC): ' + grTask.sys_created_on);
    gs.print('Updated on (UTC): ' + grTask.sys_updated_on);

    // Convert sys_created_on and sys_updated_on to the current user's time zone (Canada Mountain Time)
    var createdDate = new GlideDateTime(grTask.sys_created_on);
    var updatedDate = new GlideDateTime(grTask.sys_updated_on);

    gs.print('Created on (Local): ' + createdDate.getDisplayValue()); // Shows time in the user’s time zone
    gs.print('Updated on (Local): ' + updatedDate.getDisplayValue()); // Shows time in the user’s time zone
}

 

If my reply helped with your issue please mark helpful 👍and correct ✔️if your issue is resolved.

 

By doing so you help other community members find resolved questions which may relate to an issue they're having

 

Thanks,

Astik

Thank you so much Astik, it worked.