« Back to Index

[Bash Date Formatting]

View original Gist on GitHub

Tags: #bash #date #gnu #posix

Bash Date Formatting.sh

# when using UTC timezone, always add 1 hour to the intended hour
# so if you want 1hr in the future UTC, then use +2H
# you'll notice this when you switch between BST (no -u flag) and UTC

# macOS (standard: posix install)
#
# -u:  utc timezone
# -v:  adjust current datetime
# +%s: convert output to seconds since epoch
date -u -v +1H +%s

# macOS (gnu coreutils installed)
#
# -u:  utc timezone
# -d:  adjust specified datetime
# +%s: convert output to seconds since epoch
gdate -u -d "today 1 hour" +%s

# linux (standard: gnu coreutils)
# 
# automatically defaults to utc timezone
#
# -d:  adjust specified datetime
# +%s: convert output to seconds since epoch
date -d "today 1 hour" +%s

# You can go the reverse direction (timestamp -> date)
date -d @1267619929 # Wed Mar  3 12:38:49 UTC 2010
gdate -d @1267619929 -u # UTC flag required for macOS gnu coreutils version
date -r 1267619929 -u # macOS standard posix install