RegistriesTime The Time registry provides tools to manage and manipulate dates, times, and time-related calculations, making it easy to handle time-based data in your projects.
You can easily import all the functions from the time
registry by including the following import statement in your code
Copy import "github.com/go-sprout/sprout/registry/time"
date
The function formats a given date or the current time into a specified format string.
Copy Date(layout string, date any) (string, error)
Template Example
Copy {{ "2023-05-04T15:04:05Z" | toDate "2006-01-02T15:04:05Z" | date "Jan 2, 2006" }} // Output: "May 4, 2023"
dateInZone
The function formats a given date or the current time into a specified format string for a specified timezone.
Copy DateInZone(layout string, date any, zone string) (string, error)
Template Example
Copy {{- $date := "2023-05-04T15:04:05Z" | toDate "2006-01-02T15:04:05Z" -}}
{{ dateInZone "Jan 2, 2006" $date "UTC" }} // Output: "May 4, 2023"
{{- $date := "2023-05-04T15:04:05Z" | toDate "2006-01-02T15:04:05Z" -}}
{{ dateInZone "Jan 2, 2006" $date "invalid" }} // Error
duration
The function converts a given number of seconds into a human-readable duration string.
Copy Duration(sec any) string
Template Example
Copy {{ 3661 | duration }} // Output: "1h1m1s"
dateAgo
The function calculates the time elapsed since a given date.
Copy DateAgo(date any) string
Template Example
Copy {{- $date := now | dateModify "-10h" -}}
{{ $date | dateAgo }} // Output: 10h0m0s
dateAgo
can receive multiples input types like:
time.Time
object (object or pointer)
int
, int32
, int64
converted as Unix
now
The function returns the current time.
Template Example
Copy {{ now }} // Output(will be different): "2023-05-07T15:04:05Z"
unixEpoch
The function returns the Unix epoch timestamp for a given date.
Copy UnixEpoch(date time.Time) string
Template Example
Copy {{ now | unixEpoch }} // Output(will be different): 1683306245
dateModify
The function adjusts a given date by a specified duration, returning the modified date. If the duration format is incorrect, it returns the original date without any changes, in case of must version, an error is returned.
Copy DateModify(layout string, date time.Time) (time.Time, error)
Template Example
Copy {{- $date := "2023-05-04T15:04:05Z" | toDate "2006-01-02T15:04:05Z" -}}
{{ $date | dateModify "48h" }}
// Output: 2023-05-06 15:04:05 +0000 UTC
{{- $date := "2023-05-04T15:04:05Z" | toDate "2006-01-02T15:04:05Z" -}}
{{ $date | dateModify "0z+" }}
// Error
durationRound
The function rounds a duration to the nearest significant time unit, such as years or seconds.
Copy DurationRound(duration any) string
Template Example
Copy {{ "9600h" | durationRound }} // Output: "1y"
{{ "960h" | durationRound }} // Output: "1mo"
{{ "192h" | durationRound }} // Output: "8d"
{{ "3600s" | durationRound }} // Output: "60m"
{{ "300s" | durationRound }} // Output: "5m"
{{ "61s" | durationRound }} // Output: "1m"
{{ "59s" | durationRound }} // Output: "59s"
htmlDate
The function formats a date into the standard HTML date format (YYYY-MM-DD).
Copy HtmlDate(date any) (string, error)
Template Example
Copy {{- $date := "2023-05-04T15:04:05Z" | toDate "2006-01-02T15:04:05Z" -}}
{{ $date | htmlDate }} // Output: "2023-05-04"
This basically call dateInZone("2006-01-02", date, "Local")
htmlDateInZone
The function formats a date into the standard HTML date format (YYYY-MM-DD) based on a specified timezone.
Copy HtmlDateInZone(date any, zone string) (string, error)
Template Example
Copy {{- $date := "2023-05-04T15:04:05Z" | toDate "2006-01-02T15:04:05Z" -}}
{{ htmlDateInZone $date "UTC" }} // Output: "2023-05-04"
This basically call dateInZone("2006-01-02", date, zone)
Last updated 2 months ago