Using “date”
We know it is very common you need various output from “date” command on Linux platform and here are some useful samples you can use.
Generate some readable output
// original output
$ date
Mon Aug 26 03:59:04 UTC 2024
// get the output by giving human-readable parameter
$ date '+%Y-%m-%d %H:%M:%S' -d "2 days ago"
$ date '+%Y-%m-%d %H:%M:%S' -d "-1 day"
$ date '+%Y-%m-%d %H:%M:%S' -d "5 days"
$ date -d "yesterday"
// get the date by giving epoch value
$ date -d @1665046672
// get the epoch value
$ date ; date '+%s'
Mon Aug 26 04:06:12 UTC 2024
1724645172
$ date -d @1724645172
Mon Aug 26 04:06:12 UTC 2024
$ date -d '5 days ago'
Wed Oct 5 06:10:00 UTC 2022
$ date -d '5 days ago' '+%s'
1664950211
// get the local date/time for particular Timezone
TZ='America/Los_Angeles' date +'%Y-%m-%d %H:%M
// normally, TZ can be found in /usr/share/zoneinfo/