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