1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
|
# Date Functions
## now
The current date/time. Use this in conjunction with other date functions.
## ago
The `ago` function returns duration from time.Now in seconds resolution.
```
ago .CreatedAt"
```
returns in `time.Duration` String() format
```
2h34m7s
```
## date
The `date` function formats a date.
Format the date to YEAR-MONTH-DAY:
```
now | date "2006-01-02"
```
Date formatting in Go is a [little bit different](https://pauladamsmith.com/blog/2011/05/go_time.html).
In short, take this as the base date:
```
Mon Jan 2 15:04:05 MST 2006
```
Write it in the format you want. Above, `2006-01-02` is the same date, but
in the format we want.
## dateInZone
Same as `date`, but with a timezone.
```
dateInZone "2006-01-02" (now) "UTC"
```
## duration
Formats a given amount of seconds as a `time.Duration`.
This returns 1m35s
```
duration "95"
```
## durationRound
Rounds a given duration to the most significant unit. Strings and `time.Duration`
gets parsed as a duration, while a `time.Time` is calculated as the duration since.
This return 2h
```
durationRound "2h10m5s"
```
This returns 3mo
```
durationRound "2400h10m5s"
```
## unixEpoch
Returns the seconds since the unix epoch for a `time.Time`.
```
now | unixEpoch
```
## dateModify, mustDateModify
The `dateModify` takes a modification and a date and returns the timestamp.
Subtract an hour and thirty minutes from the current time:
```
now | date_modify "-1.5h"
```
If the modification format is wrong `dateModify` will return the date unmodified. `mustDateModify` will return an error otherwise.
## htmlDate
The `htmlDate` function formats a date for inserting into an HTML date picker
input field.
```
now | htmlDate
```
## htmlDateInZone
Same as htmlDate, but with a timezone.
```
htmlDateInZone (now) "UTC"
```
## toDate, mustToDate
`toDate` converts a string to a date. The first argument is the date layout and
the second the date string. If the string can't be convert it returns the zero
value.
`mustToDate` will return an error in case the string cannot be converted.
This is useful when you want to convert a string date to another format
(using pipe). The example below converts "2017-12-31" to "31/12/2017".
```
toDate "2006-01-02" "2017-12-31" | date "02/01/2006"
```
|