Script returning wrong time after daylight savings time change

Scott Eaglesham
Tera Contributor

I am in GMT time zone and moved forward 1 hour to daylight savings time yesterday, so instance time is now UTC+1.

 

Using the following...

 

var starttime = new GlideDateTime(starttimestring)
gs.log('starttime: ' + starttime)
return starttime
 

...(where starttimestring=2025-04-02 19:00:00) in Planned Start Date field in Create Change Request Record in a flow, set the field correctly to 02/04/2025 19:00:00 before daylight savings time change, but now sets the field to 02/04/2025 20:00:00. System log shows 'starttime: 2025-04-02 19:00:00'.

 

 
On reading some other posts on similar issues, I tried the following...
 
var starttime = new GlideDateTime(starttimestring)
gs.log('starttime: ' + starttime)
var starttime1 = new GlideDateTime(starttime.getDisplayValueInternal())
gs.log('starttime1: ' + starttime1)
return starttime1
 
...which results in system log entries 'starttime: 2025-04-02 19:00:00', and 'starttime1: 2025-04-02 20:00:00' and sets the field to 02/04/2025 21:00:00.
 
Can anyone advise a way to ensure the field is always set to correct local time?
1 ACCEPTED SOLUTION

Robert H
Mega Sage

Hello @Scott Eaglesham ,

 

It depends a bit on which time zone you mean when you say "local time". A fixed time zone (yours, GMT), or the local time zone of the user who triggers the flow?

 

In the first case you can use the following:

var starttimestring = '2025-04-02 19:00:00';
var starttime = new GlideDateTime(starttimestring);
starttime.setTimeZone('Europe/London');
starttime.subtract(starttime.getTZOffset());
grChangeRequest.start_date = starttime;

 

In the second case remove the "setTimeZone" part.

 

Regards,

Robert

View solution in original post

11 REPLIES 11

Great !! @Scott Eaglesham 

 

This function for DST will definitely be helpful. 

 

Kindly mark my answer as helpful and accept solution if it helped you in anyway. This will help me be recognized for the efforts and also move this questions from unsolved to solved bucket. 

 

Regards,

 

Shivalika 

 

My LinkedIn - https://www.linkedin.com/in/shivalika-gupta-540346194

 

My youtube - https://youtube.com/playlist?list=PLsHuNzTdkE5Cn4PyS7HdV0Vg8JsfdgQlA&si=0WynLcOwNeEISQCY

Hello @Scott Eaglesham ,

 

Please note that this will only work as expected if you, or some other user who has "GMT" as their time zone, runs this script. If someone from for example Hongkong ran this script, isDST() would always return false because there is no DST in that time zone.

 

Internally, ServiceNow always uses UTC for date/time fields, and the string passed to GlideDateTime will always be interpreted as per UTC. So if you need a robust solution that always shows the date given in your string to a GMT user, no matter who ran the script, please check my original response.

 

Regards,

Robert

I second that !!

Scott Eaglesham
Tera Contributor

Thanks for the further info @Robert H. So if, for example, a Hong Kong based user ran the script using isDST(), which, as you say, always returns false for them, the field would be set incorrectly to UTC time instead of Hong Kong time?

Scott Eaglesham
Tera Contributor

Also, would removing the "setTimeZone" part in your script work for all timezones, indluding GMT?