- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-02-2024 12:52 AM
We try to set the date and time for a "glide_date_time" type variable from script and it's adding 1 hour more automatically. Our instance is in Europe/Zurich timezone.
Steps to reproduce:
This is a sample code that we use in background script:
var gr = new GlideRecord("incident");
if(gr.get("949a64891b5af5101bc11f81bb4bcbd5"));
{
gr.setValue("due_date","10.11.2023 02:00:00");
gr.update();
}
The variable "due_date" value is set to "10.11.2023 03:00:00" instead of "10.11.2023 02:00:00". Please advise.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-03-2024 05:47 AM - edited ‎02-03-2024 06:00 AM
What you should do is either:
- use method setValue but the string 2023-11-10 01:00:00 (which is in the time zone and format expected by that API), or
- use method setDisplayValue and the string 10.11.2023 02:00:00, but this will - of course - only work correctly if the current user's selected format is DD.MM.YYYY. E.g. if the code is not adapted to match the current user's selected format, if someone selects for itself format MM.DD.YYYY, this will be interpreted by the system as 11th of Oct. vs. 10th of Nov.
Longer story (at to why):
Date/time fields store their values in UTC always.
So when one calls GlideRecord's setValue method it is understood by the system that the date/time string provided as parameter contains the date/time value in UTC.
That is why when the date/time is set as 02:00:00 and afterwards it is displayed for a user that has its time zone configured for Zürich, it is displayed as 03:00:00 - Swiss being UTC+1.
One more important detail, when one sets the value using method setValue, it is expected that the date/time string contains the date/time value in format YYYY-MM-DD HH:mm:ss.
If that is not the case, the system basically tries multiple formats trying to guess(!) what the date/time value is.
As suggested in GlideDateTime's documentation - which facilitates scripting fields of type Date/time.
So the code does not use method setValue as designed:
- it provides the date/time value in a different time zone than UTC and
- it uses o non-standard format
But all hope is not lost, there are more ways to set the value of a date/time field which does not expect UTC and system format:
- by calling GlideRecord's setDisplayValue method and providing as parameter a date/time string that contains the date/time value in the current user's time zone and the current user's format.
In that case the code could have been:
gr.setDisplayValue("due_date", "10.11.2023 02:00:00");
- by providing a GlideDateTime as value to be set; in this case it is possible to set the value of the GlideDateTime object in advance, in various combinations of formats and time zones.
E.g:
var $gdt = GlideDateTime();
$gdt.setDisplayValue('10-Nov-2024 23:59:59', 'dd-MMM-yyyy HH:mm:ss');
gr.setValue('due_date', $gdt);​
This would set the UTC time value of the field to 22:59:59 - assuming the current user has selected a Swiss time zone in its profile.
One could also change the time zone "used" by a the GlideDateTime object from the current user's time zone to and arbitrary one, as described by @Sai Shravan (applying the suggested corrections).
In that case the date/time string should contain the date/time value in that arbitrary time zone.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-03-2024 03:32 AM
Hello @tikkaa ,
You can explicitly set the time zone of your date and time object to Coordinated Universal Time (UTC). This should prevent any automatic adjustments based on the local time zone. Here's how you can modify your script:
var gr = new GlideRecord("incident");
if (gr.get("949a64891b5af5101bc11f81bb4bcbd5")) {
// Set due_date with the time zone as UTC
var dueDate = new GlideDateTime();
dueDate.setTZ("UTC");
dueDate.setDisplayValue("10.11.2023 02:00:00");
// Now set the value to the GlideRecord field
gr.setValue("due_date", dueDate);
gr.update();
}
By explicitly setting the time zone to UTC, it should maintain the exact time you specify without any automatic adjustments. Remember to adjust the time according to your needs and thoroughly test to ensure it behaves as expected.
Shravan
Please mark this as helpful and correct answer, if this helps you
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-03-2024 06:00 AM
This code is not correct in more than one way:
- GlideDateTime does not have a method named setTZ that expects a string as parameter; it does have a method named setTZ, but it expects as parameter an object of type Packages.sun.util.calendar.ZoneInfo; e.g:
var $gdt = GlideDateTime();
var tz = Packages.java.util.TimeZone.getTimeZone('UTC');
$gdt.setTZ(tz);​
Of course since Packages are not allowed in scopes, but also because setTZ is not available in scopes, the code above will only work in global scope.
- If one sets the time zone of a GlideDateTime to UTC and than sets the value in local time zone, the result will still not be OK; following your example (assuming the code is corrected as described above), a user who sets its time zone to Zürich will be shown date/time 10.11.2023 03:00:00.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-03-2024 05:47 AM - edited ‎02-03-2024 06:00 AM
What you should do is either:
- use method setValue but the string 2023-11-10 01:00:00 (which is in the time zone and format expected by that API), or
- use method setDisplayValue and the string 10.11.2023 02:00:00, but this will - of course - only work correctly if the current user's selected format is DD.MM.YYYY. E.g. if the code is not adapted to match the current user's selected format, if someone selects for itself format MM.DD.YYYY, this will be interpreted by the system as 11th of Oct. vs. 10th of Nov.
Longer story (at to why):
Date/time fields store their values in UTC always.
So when one calls GlideRecord's setValue method it is understood by the system that the date/time string provided as parameter contains the date/time value in UTC.
That is why when the date/time is set as 02:00:00 and afterwards it is displayed for a user that has its time zone configured for Zürich, it is displayed as 03:00:00 - Swiss being UTC+1.
One more important detail, when one sets the value using method setValue, it is expected that the date/time string contains the date/time value in format YYYY-MM-DD HH:mm:ss.
If that is not the case, the system basically tries multiple formats trying to guess(!) what the date/time value is.
As suggested in GlideDateTime's documentation - which facilitates scripting fields of type Date/time.
So the code does not use method setValue as designed:
- it provides the date/time value in a different time zone than UTC and
- it uses o non-standard format
But all hope is not lost, there are more ways to set the value of a date/time field which does not expect UTC and system format:
- by calling GlideRecord's setDisplayValue method and providing as parameter a date/time string that contains the date/time value in the current user's time zone and the current user's format.
In that case the code could have been:
gr.setDisplayValue("due_date", "10.11.2023 02:00:00");
- by providing a GlideDateTime as value to be set; in this case it is possible to set the value of the GlideDateTime object in advance, in various combinations of formats and time zones.
E.g:
var $gdt = GlideDateTime();
$gdt.setDisplayValue('10-Nov-2024 23:59:59', 'dd-MMM-yyyy HH:mm:ss');
gr.setValue('due_date', $gdt);​
This would set the UTC time value of the field to 22:59:59 - assuming the current user has selected a Swiss time zone in its profile.
One could also change the time zone "used" by a the GlideDateTime object from the current user's time zone to and arbitrary one, as described by @Sai Shravan (applying the suggested corrections).
In that case the date/time string should contain the date/time value in that arbitrary time zone.