logo

NJP

Date Times and Timzones

Import · Feb 10, 2022 · article

I wanted to share something that I figured out recently, in the hopes that it will help others.As everyone knows, dealing with DateTime values are very difficult and a common challenge that comes up is being able to convert a DateTime from one TimeZone to another.

I've read many articles on how to accomplish this and almost all of them involve using a Package Call, or won't work in a scoped app or involve lengthy code.

Well, I'm here to tell you, this can be accomplished with a few lines of code and works in a scoped app.

The class, GlideScheduleDateTime has a method called convertTimeZone which takes two arguments.

The first is the TimeZone you want to convert from and the second is the TimeZone you want to convert to.

Listed below is example code which can be adjusted based on your use case.

var tzConvert = new GlideScheduleDateTime("2021-12-24 08:00:00");
tzConvert.convertTimeZone("US/Central", "Etc/UTC");
gs.info(tzConvert);

DISCLAIMER: This method is used in a few places OOB, but is undocumented, so use at your own risk, as it might change or become deprecated in the future.

Labels:

image

View original source

https://www.servicenow.com/community/now-platform-articles/date-times-and-timzones/ta-p/2307140