Get Device Time Zone Offset and Working with UTC Time

Many times it is best to work with UTC time when dealing with data. Especially when data may be sourced or used in multiple time zones.

I could not find any easy solution for working with time zones and UTC time. There were some extensions, but I did not want to unnecessarily add an extension. There are also some solutions using web calls, but there is no guarantee a device will have current connectivity to the Internet. Therefore, I put together a block of code to get the timezone offset.

The following block will return the device time zone offset in milliseconds. It uses the Clock component, so make sure that component is added to your project.

Below I will break down what this block is doing.

First retrieve the time zone offset using Format Date with the Pattern β€œZ”. β€œZ” will return a signed four digit number representing the time offset in using the device time retrieved from the Clock1 Now block. For example β€œ-0430” for 4 hours and 30 minutes earlier than UTC time.

blocks

We will take to quotient of the four digit number divided by 100 to give us the hours of off set. For example -0430 will result in β€œ-4”.

blocks

Then we can’t forget about the time zones with a half hour offset. Taking the remainder when divided by 100 will give us the minutes of off set. For example -0430 will result in β€œ-30”

blocks

We then take the resulting minutes and divide by 60 to convert it to hours. Example -30 / 60 will result in β€œ-0.5”.

blocks

Then add the hours and minutes together to get total offset in hours. Example -4 plus -0.5 result is β€œ-4.5”.

blocks

Multiplying the total hours by 3600000 will convert hours to milliseconds. 60 minutes x 60 seconds x 1000 milliseconds.

blocks

Now you have the device time offset from UTC in milliseconds. For convenience, I store the result in a global variable.

blocks (1)

Now to get UTC time, take the time and subtract the time zone offset. The following will display the current UTC time.

To convert UTC time to the device time (user’s time zone), just add the stored time to the time zone offset.

Hopefully this can help save you some time in working with UTC time. Happy koding! :kodular:

3 Likes

Another way maybe is … ?

2 Likes

As you have shown @dora_paz, multiple ways it can be done. The key here is the pattern β€œZ” to get the time zone offset. When working with numbers, my personal preference is to use math rather than text segments. However, segments work fine when numbers are a fixed length.

Your solution provides one direction which may work for some use cases. In my application, my source data is in UTC and I needed a way to display it to the user in their local time. I also needed a way to take local time and save it as UTC. With one line of code, I was able to save a global value for the offset in milliseconds and then convert from UTC or to UTC with a simple addition or subtraction.

Your code does have a minor flaw in the logic. You are only capturing one digit for the hours and minutes. Your hours will only work when timezone is less than 10 hours off UTC. An you are only getting the last digit for the minutes which will always be zero. Just need to change your start for hours to 2 and change to 4 for minutes, then adjust the lengths to 2. That should fix it, but I have not tested it.

1 Like

Fixed it :slight_smile:

2 Likes