Hi guys,
inside a custom annotation in Grafana Alerting i have:
The current cooling liquid level is under 91% of tank's capacity. Detected liquid level: {{index $values "coolingliquid"}} mm Refilling the cooling liquid is recommended. Last refill date: {{ index $values "lastrefill"}} Critical level will be reached on {{ index $values "criticaldate"}}
{{ index $values “lastrefill”}} and {{ index $values “criticaldate”}} are both timestamps in ns, is there a way to format the timestamps into strings with format YYYY-MM-DD HH:mm:ss.
I tried in a lot of methods that should work with Go Template Language but on Grafana they aren’t working.
I’d try using toTime
, which transforms the timestamp in seconds to Time type.
Typically, a timestamp in nanoseconds has 19 digits and 10 digits in seconds. You’ll need to get the first 10 digits using reReplaceAll
and use then toTime
. For instance:
{{ toTime (reReplaceAll "^(\\d{10})\\d{9}$" "$1" "1684300000000000000") }}
This prints 2023-05-17 05:06:40 +0000 UTC
toTime
returns a Time object, so you can use Time functions to display your desired format:
{{ ( toTime (reReplaceAll "^(\\d{10})\\d{9}$" "$1" "1684300000000000000") ).Format "2006-01-02T15:04:05" }}
This prints 2023-05-17T05:06:40
Yeah, I managed to find the function you said in the documentation and i fixed the issue, thank you!
1 Like