Date in Template from Data (json)

Hi,

I have a set of data files in json format. Each one contains a published field in the JSON syntax, the date is represented as a string in RFC3339 format, viz:

"published": "2018-05-09T20:21:51+01:00"

I have tried to use this date in my data templates, showing just the Y-M-D, but

{{ dateFormat "2006-02-01" .published }}

Errors out with:

<dateFormat "2006-01-...>: error calling dateFormat: unable to cast false of type bool to Time

I am aware that I need to convert the string from .published to a time.Time item, I just don’t know where or how to do that. I’ve checked here and on github, but am now at a loss. I know the template is getting the string value, as if I just put {{ .published }} in, that comes up perfectly, but is not acceptable.

Any ideas?

Hello! :slight_smile:

I haven’t tried, but this might work:

{{ dateFormat "2006-02-01" (time .published) }}
4 Likes

thanks for responding kaushal

error calling dateFormat: unable to cast errors.errorString{s:"unable to cast false of type bool to Time"} of type errors.errorString to Time

:confounded:

Try this

{{ $published := time .Params.published }}
{{ dateFormat "2006-02-01" $published  }}

I used a frontmatter value, didn’t try a json parsed one, sorry.

Yeah, sjardim, same issue as the prior solution.

It works with frontmatter values, but it doesn’t work with $.Site.Data values.

There’s an old github issue where @bep says that “that isn’t a date, its a string”; but there’s no information there on how to cast that string from a data file into a date.

JSON, YAML etc. has date types. They are not qouted. If you quoute the value, it is a string and you need to convert it from a string to a date (using the date func).

Did you mean time? If not, I don’t find the date function documented.

Is one of the published values in the JSON a bool? Can you share that JSON file? It will be easier to debug that way.

Yes…

Thats… that’s precisely the question I’m asking @bep.

How do I convert it from a string, to a date.

So I just tried it out… Using time as I suggested earlier, it works.

To the error that you posted earlier:

error calling dateFormat: unable to cast errors.errorString{s:“unable to cast false of type bool to Time”} of type errors.errorString to Time

… you just have a non-date-string value of “published” somewhere in your JSON.

Here’s how I tested… with this in data/somedata.json:

{
  "published": "2018-05-09T20:21:51+01:00"
}

and

this in any template file like single.html:

{{ with .Site.Data.somedata }}
    <p>
        {{ partial "debugprint.html" . }}
    </p>
    <p>
        {{ partial "debugprint.html" .published }}
    </p>
    <p>
        {{ partial "debugprint.html" (time .published) }}
    </p>
    <p>
        {{ partial "debugprint.html" (dateFormat "2006-02-01" (time .published)) }}
    </p>
{{ end }}

I get

image


debugprint.html partial

1 Like

You are completely correct.

Unfortunately, I was dealing with 900+ data files, all of them unmunged json.

I wrote a quick php script, 5 had empty publisheds.

Thanks @kaushalmodi

1 Like