Get the current date and time in a specific timezone offset

Currently (:wink:), the {time} command will get you the time in your current timezone. However, sometimes you want to get the time in a specific time offset. This is especially useful if you have multiple users running the same snippet and each one of them is in a different timezone.

The following snippet will always give you the current time in a specific timezone offset (in this case, India Standard Time), irrespective of your current timezone:

{get_current_time_at_offset=(desired_offset)->block
var desired_offset = replace(desired_offset, ":", "") # remove the colon if the user added it
if len(desired_offset) <> 5 # +0100 for example
return "Invalid input"
endif
# converts the offset to minutes from UTC. For example:
# +0530 is 330 minutes ahead of UTC
var offset_to_minutes = offset -> block
var seconds = substring(offset, 2, 2) * 60 + substring(offset, 4, 2)
return seconds if left(offset, 1) = "+" else -seconds
endblock
var current_offset = offset_to_minutes({time: ZZ})
desired_offset = offset_to_minutes(desired_offset)
var additional_shift = desired_offset - current_offset
return {time: X; shift={=additional_shift}m}
endblock}
Time in India Standard Time: {time: LLL; at={=get_current_time_at_offset("+0530")}; pattern=X}

You only need to change the last line for your specific case. When you click on that command in your snippet editor, you will get the option to choose your format, as shown in this image:

image

You can choose YYYY-MM-DD or HH:mm or any format as you'd like. Secondly, to change the target timezone offset, scroll down and look for the at setting, and change the offset in it:

image

Note that for timezones that have daylight savings time, the offset will change twice every year,. You need to manually update that in the snippet. For example: Pacific Timezone flips between -0700 and -0800 every year.


As an advanced use case, to get a specific time in a specific timezone offset, you can try out this snippet:

{formdate: X; name=time_input}
{get_specific_time_at_offset=(time_value, desired_offset)->block
var desired_offset = replace(desired_offset, ":", "") # remove the colon if the user added it
if len(desired_offset) <> 5 # +0100 for example
return "Invalid input"
endif
# converts the offset to minutes from UTC. For example:
# +0530 is 330 minutes ahead of UTC
var offset_to_minutes = offset -> block
var seconds = substring(offset, 2, 2) * 60 + substring(offset, 4, 2)
return seconds if left(offset, 1) = "+" else -seconds
endblock
var current_offset = offset_to_minutes({time: ZZ})
desired_offset = offset_to_minutes(desired_offset)
var additional_shift = desired_offset - current_offset
return {time: X; at={=time_value}; pattern=X; shift={=additional_shift}m}
endblock}
Time in India Standard Time: {time: LLL; at={=get_specific_time_at_offset(time_input, "+0530")}; pattern=X}

It still works the same as before, and you can input the current time in the date picker at the top.

3 Likes