I've added an "updated" parameter to some of my posts, it would be nice to format them the same way as I format my date.
And on that note, in posts it could be nice to write dates in ISO 8601 and have them formatted with day and month name. ie. {{ 2014-03-20 | format "Monday, Jan 2, 2006" }}
Should be pretty straightforward to add. See the examples here… https://github.com/spf13/hugo/blob/master/template/bundle/template.go#L161
You’ll probably need this function as well… https://github.com/spf13/hugo/blob/master/hugolib/metadata.go#L38
Care to give it a try and make a pull request? Please call it dateFormat instead of format.
The TOML format supports dates natively as well. If you use that it should just work, however I see value in adding this feature. Â
If you do write it, please add it to the docs so others know how to use it. The appropriate page is http://hugo.spf13.com/layout/functions
Best,
Steve
--Â
Steve Francia
http://stevefrancia.com
http://spf13.com
http://twitter.com/spf13
From:Â Even Alander [email protected]
Reply:Â spf13/hugo [email protected]
Date:Â March 20, 2014 at 5:07:08 PM
To:Â spf13/hugo [email protected]
Subject:Â [hugo] format string as date (#235)
I've added an "updated" parameter to some of my posts, it would be nice to format them the same way as I format my date.
And on that note, in posts it could be nice to write dates in ISO 8601 and have them formatted with day and month name. ie. {{ 2014-03-20 | format "Monday, Jan 2, 2006" }}
—
Reply to this email directly or view it on GitHub.
This issue was recently brought up on the discussion forum:
Interesting. I'll try to write it later (tonight)
For reference: There has been quite a bit of refactoring since @spf13's reply last March, and the links are no longer accessible. Here are the new links.
Should be pretty straightforward to add. See the examples here…
https://github.com/spf13/hugo/blob/master/template/bundle/template.go#L161
Permalink of the historic file: https://github.com/spf13/hugo/blob/64572d2d60a6bee0cc0b87dcf0a944d88375d964/template/bundle/template.go#L161
Steve was referring to the template funcMap defined in NewTemplate(). The same but expanded funcMap is now in init() in tpl/template.go:
You’ll probably need this function as well…
https://github.com/spf13/hugo/blob/master/hugolib/metadata.go#L38
Permalink of the historic file: https://github.com/spf13/hugo/blob/2540d884d8d8566caa1cfb71a23f01a746133581/hugolib/metadata.go#L38
Steve was referring to func stringToDate(s string) (time.Time, error) contained therein. This function has been renamed to StringToDate() and now resides in caste.go in "github.com/spf13/cast":
@tatsushid wrote:
Interesting. I'll try to write it later (tonight)
What a pleasant surprise! :+1:
Thanks for the reference. It's pretty easy work because of cast library, just I wrote
func DateFormat(layout string, v interface{}) (string, error) {
t, err := cast.ToTimeE(v)
if err != nil {
return "", err
}
return t.Format(layout), nil
}
If someone is interested in treating an int value as Unix time value and converting it into a string, I'll send PR to cast library
@tatsushid, great job! Thank you!
Your implementation of dateFormat looks good to me! @deificx's example works with minor modification, i.e. the string needs to be quoted:
{{ "2014-03-20" | dateFormat "Monday, Jan 2, 2006" }}
This works too:
{{ with .Params.startdate }}
{{ . | dateFormat "Monday, Jan 2, 2006" }}
{{ end }}
What do you think, @spf13 and @bjornerik? This one is slightly beyond my ken, so it would be great if you could do the actual merging. :wink:
Cheers,
Anthony
I think it looks exactly as it should be. I can merge this.
Merged in 37490ee
:clap:
Guys was this always intended to not work with parsing datetime stamps with time? For example, {{ "2014-03-20 05:00PM" | dateFormat "Monday, Jan 2, 2006" }} fails. I have a need for time parsing as well as date and not finding it without a custom filter. Must be something simple. Thanks in advance.
@robmuh use http://discuss.gohugo.io/ for questions.
Most helpful comment
Guys was this always intended to not work with parsing datetime stamps with time? For example,
{{ "2014-03-20 05:00PM" | dateFormat "Monday, Jan 2, 2006" }}fails. I have a need for time parsing as well as date and not finding it without a custom filter. Must be something simple. Thanks in advance.