Date Formatting


#1

I am trying to format date in a specific time zone using the format string of 02 Jan 2006 15:04 MST. This works locally on my machine (11 Sep 2017 08:00 MDT) because I suppose I am in the MST zone but when published to netlify, their UTC servers render the size with 11 Sep 2017 08:00 -0600

Is there a way to get the date to format similar to on my local machine when published? Thanks.


#2

It looks like your input doesn’t specify a time zone, so couldn’t you just do something like {{ printf "%s MST" (dateFormat "02 Jan 2006 15:04" $input) }}?

The other option would be to use the TZ environment variable. Take note of the last paragraph there; you can set TZ to something like “America/Denver”.


#3

I have a date string date = 2017-10-02T19:10:38-06:00

Running locally this displays as 2017 Oct 10 7:10 PM

I am using the format style of DateForm = "2006 Jan 01 3:04 PM"

{{- .Date.Format .Site.Params.DateForm -}} is being used to render the date.

Why is this displaying a 8 days in the future?

Does it really need to be DateForm = "2006 Jan 02 3:04 PM" for this to format properly?


#4

Go by Example: Time Formatting / Parsing:

Layouts must use the reference time Mon Jan 2 15:04:05 MST 2006 to show the pattern with which to format/parse a given time/string. The example time must be exactly as shown: the year 2006, 15 for the hour, Monday for the day of the week, etc.


#5

On the archetypes page it mentions

date and title are the variables that ship with Hugo and are therefore included in all content files created with the Hugo CLI. date is generated in RFC 3339 format by way of Go’s now() function, which returns the current time.

Is there a way to make the CLI use a format when doing that? I would like that time to be in my TZ and not UTC?


#6

Yep! A while ago archetype customization got really awesome, and the default date Hugo uses is just {{ .Date }}. You can change that in your archetype (even archetypes/default.md).

I’ll leave the formatting to you, but there is plenty of reference material. :slight_smile:


#7

@maiki Thanks for the duh reminder!

I’d like to pin the time zone for this date creation though.

Given

date = {{ dateFormat "2006-01-02T15:04:05" .Date }} => 2017-10-04T14:35:46.000Z and rendered as 2017 Oct 04 2:35 PM with the format of "2006 Jan 02 3:04 PM"

This works locally on my machine but I think when it get’s built in the cloud on a different TZ the output will be different. I’ll have to TIAS but is it possible to add the -0600 or whatever for MST?


#8

May I ask about your preference? Understanding that will help me think about this.

UTC is for this exact issue, of creating a date that is portable across systems, regardless of locality. If you are trying to get an output to format a certain way, maybe we can pull it off with the UTC value.


#9

This website and the times are only valuable in MST. I would like issues created with the hugo cli locally to display the date correctly when also built in the cloud.


#10

Just wanted to chime in that .Date is a preformatted string – if you want a proper date I would much rather do:

date = {{ now.Format "2006-01-02T15:04:05" }}