Localize UTC time with specific timezone

I have multiple pages for different communities from different timezones that are supposed to show their events with localized time. Each community has their own data/yaml file with events including start date and time.

For example, one such start time entry looks like “2025-05-17T13:00:00Z”, i.e. it is represented in UTC.

For each community page I want to specify the corresponding timezone, e.g. “Europe/Berlin” to render the start datetime in a localized and daylight savings time aware manner in the corresponding timezone.

However, when I do {{ time "2025-05-17T13:00:00Z" "Europe/Berlin" }}, I get 2025-05-17 13:00:00 +0000 UTC and not the correct localized datetime.
Also, {{ (time "2025-05-17T13:00:00Z").In "Europe/Berlin" }} fails with can't handle "Europe/Berlin" for arg of type *time.Location, which when changed to {{ (time "2025-05-17T13:00:00Z").In (time.LoadLocation "Europe/Berlin") }} just results in the next error can't evaluate field LoadLocation in type interface {}

Is what I’m trying at all possible with Hugo?

Where in the documentation did you find this?

Oh, I think I might have accidentally tried what was merely a feature proposal by a core team member: dateFormat: Force a specific timezone? - #2 by moorereason

Yes, but it’s a bit ugly:
https://discourse.gohugo.io/t/present-a-date-object-in-different-timezones/41863/

Also see related proposal:
https://github.com/gohugoio/hugo/issues/13548

This goes into the right direction indeed.

How would I then display only the hours and minutes considering the offset?
{{ $localized | time.Format "15:04" }} doesn’t seem to do the trick.

Also thanks for opening the issue on GitHub, that looks great :smiling_face:

This works fine for me:

{{ $localized := partial "time-in-location.html" (dict "time" .Date "tz" "Australia/Adelaide") }}
{{ $localized | time.Format "15:04" }}

I added another example at the bottom of the home page on the example site. Try it:

git clone --single-branch -b hugo-forum-topic-41863 https://github.com/jmooring/hugo-testing hugo-forum-topic-41863
cd hugo-forum-topic-41863
hugo server

Note that displaying time without date may be insufficient depending on source and target time zones (e.g., time.Now may be tomorrow somewhere else).

I see, {{ $temp = $time.UTC.Add (time.ParseDuration (printf "%fh" $offsetHoursFractional)) }} is what I was missing. Thanks a lot!

1 Like

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