How to display time by timezone?

Hello all,

I create a new post via hugo new post/test.md

---
date: 2023-01-27T01:09:51+08:00
---

2023-01-27T01:09:51 is UTC+0 and +08:00 is my current timezone.

I try to display it on page via {{ .Date | time.Format "2006-01-02 15:04:05" }} and it shows the results as 2023-01-27 01:09:51, but I want it to display according to my current time zone, like 2023-01-27 09:09:51.

How? Thank you so much.

Then the fully qualified time stamp should be:

2023-01-27T01:09:51-00:00

OR

2023-01-27T01:09:51Z

To display the local time equivalent:

{{ .Date.Local.Format "2006-01-02 15:04:05" }}

OR

{{ .Date.Local | time.Format "2006-01-02 15:04:05" }}

Hello, thank you so much for the reply, but it is still strange.

I updated the date to 2023-01-27T01:09:51Z, and also use local time equivalent. {{ .Date.Local | time.Format "2006-01-02 15:04:05" }}

And the time it should display is 2023-01-27 09:09:51, but actually I get 2023-01-26 15:09:51.

My local timezone is +08:00, not -08:00.

Thank you.

Which operating system?

CloudFlare pages.

You need to set the TZ to “Asia/Singapore” in your Cloudflare build settings. This is not a Hugo issue.

This screen capture may be helpful. Not sure how old it is…

I see, thank you so so much.

There is an unknown issue with timezones related to Go Lang (see: https://community.cloudflare.com/t/hugo-timezone-format-issue/390678/12) (it’s also happening on Netlify). Check the following threads:

There are two workarounds:

Workaround 1: Set timezone var in CF Pages

See @jmooring’s screenshot.

Only catch, if you want to display different timezones (for example, for different languages), this will mess things up with the other timezones. The timezone you set in CF Pages is fixed. If you set it, everything will be in that timezone.

Workaround 2: edit your layout files

Add this in your layout files where the date / time is publicly displayed: {{ time.Format 2006-01-02T15:04:05Z07:00 (.PublishDate.UTC.Add (time.ParseDuration $.Site.Language.Params.tzOffset)) }}

By using Hugo’s / GoLang’s .UTC, the post dates are automatically calculated to UTC time. From there, the .Add feature in combination with time.ParseDuration will add/subtract additional hours based on the value of $.Site.Language.Params.tzOffset.

So, if you want a post (or another language) to display its local timezone, just set the post’s frontmatter to the correct one, say: date: 2023-01-27T01:09:51+09:00.

The value for the $.Site.Language.Params.tzOffset is based on what you’ve included in your [languages] config section. It’s custom, so just add a new param tzOffset per language.

You should have something like this:

{{ if not .PublishDate.IsZero }}<time datetime="{{ .PublishDate.UTC.Format "2006-01-02T15:04:05Z07:00" }}">{{ time.Format 2006-01-02T15:04:05Z07:00 (.PublishDate.UTC.Add (time.ParseDuration $.Site.Language.Params.tzOffset)) }}</time>{{ end }}

Here is what’s on my site: layouts/_default/header.html · main · YourOnly.One / site.snoworld · GitLab. And the live version is at: (en) Snoworld ❄️; (ja) 雪の世界 ❄️; (ko) 스노우 월드 ❄️.

You’ll see that the timezones are displayed correctly per language while I’m hosted on CF Pages. If you want to check the frontmatter for my posts, see: content/snoworld at main - content - Codeberg.org.

:slight_smile:

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