Template for human readable date time?

5/30/2019

{{ now }} gives: 2019-05-30 04:40:35.847147175 +0000 UTC m=+1983611.179295260

and I would like to make this more human readable by grabbing the first 2 and joining = 2019-05-30-04:40:35.847147175.

How can this be done?

Failed attempt:

time: {{ now || splitList " " | (not sure how to slice first 2) | join "-" }}
-- atkayla
go-templates
kubernetes-helm

1 Answer

5/31/2019

You should look through the whole list of Sprig date functions, where Sprig is a library of Go template functions that is included in Helm.

In particular you probably want the date function, which formats a time.Time value using a Go format string. As the linked documentation says:

...take this as the base date: Mon Jan 2 15:04:05 MST 2006 Write it in the format you want.

So try something like

{{ now | date "2006-01-02 15:04:05.000000" }}

The extended examples for the Go (*time.Time).Format function are probably relevant here too; for example, 15:04:05.999999 should drop trailing zeros if that's what you want.

-- David Maze
Source: StackOverflow