Present a date object in different timezones

How do I present a date in two different time zones?

Basically, I want to recreate the following:

package main

import (

const kyivTZ = "Europe/Kyiv"
const vancouverTZ = "America/Vancouver"

func main() {
	t := time.Now().UTC()
	locKyiv, _ := time.LoadLocation(kyivTZ)
	locVancouver, _ := time.LoadLocation(vancouverTZ)

	fmt.Printf("%v in UTC\n", t)
	fmt.Printf("%v in %v\n", t.In(locKyiv), kyivTZ)
	fmt.Printf("%v in %v\n", t.In(locVancouver), vancouverTZ)

// Output:
// 2022-12-12 04:21:11.206403 +0000 UTC in UTC
// 2022-12-12 06:21:11.206403 +0200 EET in Europe/Kyiv
// 2022-12-11 20:21:11.206403 -0800 PST in America/Vancouver

I am not sure how to do it in Hugo templates. What I have is:

{{ $time := time (now) "Europe/Kyiv" }}
{{ $time.Format "Mon, 02 Jan 2006 15:04:05 -0700" | safeHTML }}

However, it outputs the date not in the correct time zone: Sun, 11 Dec 2022 20:22:59 -0800, should be Sun, 11 Dec 2022 06:22:59 +0200 instead.

Hugo currently has no provision for converting time values from one location to another. If it’s 4:20 PM in Vancouver, Hugo has no idea what time it is in Kiev.

The time.AsTime TIMEZONE parameter is only useful for dates without offsets. The TIMEZONE parameter qualifies dates without offsets, adding an offset and time zone abbreviation.

If the date already has an offset, the TIMEZONE parameter is ignored.

Assuming you have not set a fallback timeZone in your site configuration…

Code Output
{{ time.AsTime "2022-12-11T22:36:09" }} 2022-12-11 22:36:09 +0000 UTC
{{ time.AsTime "2022-12-11T22:36:09" "Europe/Kyiv" }} 2022-12-11 22:36:09 +0200 EET
{{ time.AsTime "2022-12-11T22:36:09-08:00" "Europe/Kyiv" }} 2022-12-11 22:36:09 -0800 PST
{{ time.AsTime now "Europe/Kyiv" }} 2022-12-11 22:50:03 -0800 PST

Adding the TIMEZONE makes no difference for dates with offsets (including what the now function returns).

But Hugo does know the UTC time. So by looking at offsets we can figure it out.

This example use a partial function to return a given time in the target time zone. Twice a year it may be off by an hour in those locations that switch back and forth between STD and DST.

git clone --single-branch -b hugo-forum-topic-41863 hugo-forum-topic-41863
cd hugo-forum-topic-41863
hugo serv
1 Like

Thank you, @jmooring!

This is an excellent solution for non-critical dates.

I hope Hugo will get a (time.Time).In(timezone) function one day as the calculation of offsets is very complicated and nuanced.

Also, there is a fallback solution as to store all the dates on a page in UTC, but convert them with a front-end code like Luxon, but of course, it has its own tradeoffs.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.