dateFormat: Force a specific timezone?

@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