How to prevent Hugo from reformat time (publishDate, Date)

Because I want to load a post by js from client. I use JSON output. Below is my single.json file:

{
	"url": "{{- .Permalink -}}",
	"publish": "{{- .PublishDate -}}",
	"title": "{{- .Title -}}"
}

Well, everything is supported to be done (on Hugo side) until I found that the JSON.publish datum was converted to some other format, not my. For instance:

Here is the my time format (RFC-3339)

---
title       : Saturday
publishDate : 2020-03-15T17:59:34+07:00
---

Here is what happen after it got compiled:

{
	"url": "http://blog.ngdangtu.com/entries/saturday/query.json",
	"publish": "2020-03-15 17:59:34 +0700 +07",
	"title": "Saturday"
}

I’ve tried to reformat at the single.json file like this .PublishDate.Format .Site.Params.format.time. But the result is still the same. Did I do something wrong?

Update 15:05, 18-Mar-2020
Today, when I run Hugo again, the .PublishDate.Format .Site.Params.format.time solution works?! But if I pass raw .PublishDate, it still not work.

Yes. Please share your repo so we can assist. :slight_smile:

Thanks for the assist,
I haven’t plan to make a repo yet, it just a folder of code. Can I compress it and send it to you? If it’s okay to you, here is FF send link: https://send.firefox.com/download/bc1daaf800dd530c/#uaAsyB7l0fBjj1hpgoy59Q

My idea this time is to build a webapp-like blog. So 99% was output as JSON.

use the .Format function

But I can’t set it in archetype file because the .Date is pure string value.
In my code, I tried to set format it from the archetype, btw (sorry about this missing piece of information)

one of my archetypes:

+++
tags        = ["{{ now.Format "2006"}}"]
description = "{{ replace .TranslationBaseName "-" " " | title }}"
title       = "{{ replace .TranslationBaseName "-" " " | title }}"
date        = "{{ (.Date |time).Format "2006-01-02T15:04:05Z07:00" }}"
expirydate  = "{{ ((.Date | time ).AddDate 2 0 0).Format "2006-01-02T15:04:05Z07:00"}}"
draft       = false
series      = []
+++
## H2
**Insert Lead paragraph here.**

<!--more-->

from the config:

[frontmatter]
date                       = [ "date", ":filename", ":default"]
publishDate                = [ "publishDate", "date"]
lastmod                    = [ "lastmod", ":fileModTime", "publishDate"]
expiryDate                 = [ "expiryDate"]

Hope this helps :wink:

1 Like