Using date as a string in RFC3339 format

I need to pass on the Published and Last Modified dates for a page in the JSON syntax(“2018-05-09T20:21:51+01:00”) in the head of each html page (for Schema.org ), with the date represented as a string in RFC3339 format. I’m trying to do this with following code, without specifying a format since as I understand it Hugo formats dates in RFC3339 format by default:

"datePublished": "{{ .PublishDate }}",
 "dateModified": "{{ .Lastmod }}",

This produces the following result:

“datePublished”: “2016-06-19 11:50:32 \u002b0000 \u002b0000”,
“dateModified”: “2016-06-19 11:50:32 \u002b0000 \u002b0000”,

If I then change the code to

"datePublished": "{{ .PublishDate.Format "RFC3339" }}",
 "dateModified": "{{ .Lastmod.Format "RFC3339" }}",

I get this:

“datePublished”: “RFC1111119”,
“dateModified”: “RFC1111119”,

How do I get a result that matches the required format?

1 Like
    "datePublished": {{ .Date.Format "2006-01-02T15:04:05Z07:00" }},
    "dateModified": {{ .Lastmod.Format "2006-01-02T15:04:05Z07:00" }},

See .Format | Hugo

1 Like

Thanks very much. The above code produces the following result:

“datePublished”: “2016-06-19T11:50:32Z”,
“dateModified”: “2016-06-19T11:50:32Z”,

The Google spec suggests the following (though I suspect it won’t trigger anything drastic if the format is slightly different):

“datePublished”: “2015-02-05T08:00:00+08:00”,
“dateModified”: “2015-02-05T09:20:00+08:00”,

Dates should be formatted per ISO 8601. See:

The format string provided by @frjo produces date strings that comply with ISO 8601.

You are getting this:

2016-06-19T11:50:32Z

Because (a) you have not specified a time zone offset in your front matter dates, or (b) the time zone offset specified in your front matter dates is zero (i.e., your time zone is GMT).

If your front matter dates do not include a time zone offset, you can set a default time zone in your site configuration:

timeZone = 'America/New_York'
2 Likes

I defined the used different date formats in my hugo / config file like this

[Params]
    published                         = "2016-08-21T00:00:00Z"
    dateFormatLong              = "2006-01-02 15:04"
    dateFormatShort             = "2006-01-02"
    dateFormatYear              = "2006"
    dateFormatUTC              = "2006-01-02T15:04:05Z"
    dateFormatISO8601       = "2006-01-02T15:04:05-07:00" 
    dateFormatRFC822Z      = "02 Jan 2006 15:04:05 UT"

In my template it gets more readable

"datePublished": "{{ .PublishDate.Format site.Params.dateFormatISO8601 }}"
5 Likes

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