dateFormat: Force a specific timezone?

Here is my current dateFormat:
{{ dateFormat 3:04 PM • 02 Jan 2006" .Date.Local}}

How do I force it to resolve to a specific timezone (ET, specifically)? It uses my local time when I run Hugo locally, but Netlify builds my site with UTC, so my published blog has times from the wrong timezone.

I hate the date formatter in Go Templates, and I cannot seem to find comprehensive documentation anywhere. This is my #1 pain point when it comes to Hugo.

5 Likes

@keithjgrant,
At the moment, we don’t provide a way to set a timezone (all you can currently do is get the system time or UTC). I’ll pitch a solution below to begin a discussion on how to fix this.

Cc: @bep


We could add a time.LoadLocation template function that simply front-ends the Go stdlib. You can see a sample usage in this playground. The template would look like this:

{{ $loc := time.LoadLocation "America/Chicago" }}
System Time:  {{ time.Now }}
Central Time: {{ time.Now.In $loc }}

In your case, that would be:

{{ $loc := time.LoadLocation "America/New_York" }}
{{ dateFormat "3:04 PM • 02 Jan 2006" (.Date.In $loc) }}

That should be relatively easy to implement.

The more complicated issue would be trying to add a default location setting to the site config. In that case, we would likely want to treat all dates read from front matter as being in the default location (assuming no timezone data is present in the date string). Currently, all dates are assumed to be in the local system time. Solving that issue would take a lot more work.

4 Likes

Does setting the TZ environment variable in Netlify help? I haven’t used that var for hugo directly but it helps for setting the time zone to what I want when I export Org files to markdown for Hugo using Emacs on Netlify.

I needed setting the timezone to UTC so that my tests run on Netlify wouldn’t fail due to timezone differences.

4 Likes

Forgot about the environment. Duh. Yes, you should be able to set the TZ variable to something like America/New_York.

Excellent! Setting the TZ environment variable in Netlify worked. It looks like only EST and EDT are valid though, not ET, so I’ll have to change this every time daylight savings changes, but I guess I can live with that.

1 Like

I am such a newbie on this subject that I do not know how to add TZ environment variable. But after reading this documentation, I learned to add it to the build.environment label:

If a key has a list of key/value pairs as its value, you can set that key in its own block like this:

 [build.environment]
   VARIABLE = "value"

Thanks to @kaushalmodi !

1 Like

I’ve tried your code and the result is:

ERROR 2019/09/04 08:36:13 failed to render pages: render of “home” failed: “/…/themes/xyz/layouts/index.html:8:12”: execute of template failed: template: index.html:8:12: executing “main” at : can’t evaluate field LoadLocation in type interface {}

@starlight-tu the code you’re referencing was a proposal. It has not been implemented.

You’ll need to set the TZ environment variable as mentioned above in other posts.

1 Like

Thanks to this post, I could manage to have a correct now time on netlify by adding the TZ parameter to netlify.toml. Thanks a lot.

[context.production.environment]
TZ = "Europe/Paris"
1 Like