Blame
|
1 | # Date |
||||||
| 2 | ||||||||
|
3 | *doing fun stuff with the linux date command* |
||||||
| 4 | ||||||||
|
5 | ## Show the week number |
||||||
| 6 | ||||||||
|
7 | ```bash |
||||||
| 8 | date +%U |
|||||||
| 9 | ``` |
|||||||
|
10 | |||||||
| 11 | ## ISO8601 format |
|||||||
| 12 | ||||||||
| 13 | ```bash |
|||||||
|
14 | date --iso |
||||||
|
15 | ``` |
||||||
| 16 | ||||||||
|
17 | or using [homebrew](https://brew.sh): |
||||||
| 18 | ||||||||
| 19 | ```bash |
|||||||
| 20 | gdate --iso |
|||||||
| 21 | ``` |
|||||||
| 22 | ||||||||
|
23 | MacOS date command doesn't support `iso`, here are some useful variations: |
||||||
| 24 | ||||||||
| 25 | ```bash |
|||||||
|
26 | date "+%Y-%m-%d" |
||||||
| 27 | ``` |
|||||||
| 28 | ```bash |
|||||||
|
29 | date "+%Y%m%dT%H%M%S" |
||||||
|
30 | ``` |
||||||
| 31 | ||||||||
| 32 | ## Yesterday and Tomorrow |
|||||||
| 33 | ||||||||
| 34 | ```bash |
|||||||
| 35 | $ date +%A --date today-1days |
|||||||
| 36 | Wednesday |
|||||||
| 37 | ||||||||
| 38 | $ date +%A --date today+1days |
|||||||
| 39 | Friday |
|||||||
| 40 | ||||||||
| 41 | $ date +%A --date yesterday |
|||||||
| 42 | Wednesday |
|||||||
| 43 | ||||||||
| 44 | $ date +%A --date "2 days ago" |
|||||||
| 45 | Tuesday |
|||||||
| 46 | ``` |
|||||||
| 47 | ||||||||
| 48 | This also works as expected for other variables: |
|||||||
| 49 | ||||||||
| 50 | ```bash |
|||||||
| 51 | $ date +%Y --date "last year" |
|||||||
| 52 | 2011 |
|||||||
| 53 | ||||||||
| 54 | $ date --date "next monday" |
|||||||
| 55 | Mon Mar 19 00:00:00 EST 2012 |
|||||||
| 56 | ``` |
|||||||
| 57 | ||||||||
| 58 | Here's the entry from the `man` page: |
|||||||
| 59 | ||||||||
| 60 | ``` |
|||||||
| 61 | DATE STRING |
|||||||
| 62 | ||||||||
| 63 | The --date=STRING is a mostly free format human readable date string |
|||||||
| 64 | such as "Sun, 29 Feb 2004 16:21:42 -0800" or "2004-02-29 16:21:42" |
|||||||
| 65 | or even "next Thursday". A date string may contain items indicating |
|||||||
| 66 | calendar date, time of day, time zone, day of week, relative time, |
|||||||
| 67 | relative date, and numbers. An empty string indicates the beginning |
|||||||
| 68 | of the day. The date string format is more complex than is easily |
|||||||
| 69 | documented here but is fully described in the info documentation. |
|||||||
| 70 | ``` |
|||||||
| 71 | ||||||||
| 72 | *damn* you info docs! |
|||||||