[MERGED] Proposal: Replace .Page.Now

We currently provide access to time.Now() by way of hugolib/Page.Now(). The current system time has no real relationship to a Page, so it seems odd making it available this way. Additionally, to access the current time in a template, you must have a Page in your context (there are partial template scenarios where the Page may not be in context).

Proposal: Add a now template function and deprecate .Page.Now in v0.19.

See also: https://github.com/spf13/hugo/pull/2859

Sure, but you would have to define what to replace it with – I see a PR about UTC time and I assume we don’t want a nowLocalTime, nowUTC, nowEST etc.

The referenced PR changed from timeNowUTC to timeNow (I think he realized he could do timeNow.UTC after submitting the PR). I’m proposing we simply call the func now.

Yes, now is the first thing I would have tried without looking in the doc.

I’ve renamed timeNow to now in the pull request https://github.com/spf13/hugo/pull/2859.

@moorereason You’re right, I changed the PR from timeNowUTC to timeNow after realizing that.

Sorry if I’m missing something, but this currently works for me in 0.18.

{{ .Now.Format "2006" }}

as well as

{{ .Now.Unix }}

@budparr Yes. {{ .Now.Format "2006" }} works perfectly. But .Now is on the “Page” object, not a template function.

@moorereason’s primary reason for the discussion if I’m not mistaken is outlined in their first comment:

The current system time has no real relationship to a Page, so it seems odd making it available this way. Additionally, to access the current time in a template, you must have a Page in your context (there are partial template scenarios where the Page may not be in context).

I see now. Thanks for the clarification!

Merged. Thanks @nishanths!

Pardon my ignorance. But can we have an example please?

Currently the deprecated syntax Copyright © {{.Now.Format "2006"}} returns Copyright © 2017

How to return the same with the new function?

If I use it on its own like so Copyright © {{now}}
I get the current full time stamp Copyright © 2017-04-01 10:27:26.537572392 +0300 EEST

The docs could use an example on how to limit the scope of the new function.
Most Hugo themes that I’m currently seeing either use the deprecated syntax or have the year written manually on the footer.

(Might as well just write the year manually and get over with it. But then again one more thing to remember changing every new year).

Found the solution.

The {{ .Now.Format "2006" }} needs to change to:

{{ now.Format "2006" }}

Maybe include an example in the Docs?

2 Likes

https://hugodocs.info/functions/now/