| name | date-time-cli |
| description | Date, time, timezone, and epoch timestamp manipulation from the command line. Use when user mentions "date command", "timestamp", "epoch", "unix timestamp", "timezone conversion", "date math", "date formatting", "ISO 8601", "convert timestamp", "time ago", or any date/time manipulation in shell. |
Date & Time CLI
Current Date and Time
date
date +%s
date +%Y-%m-%d
date +"%Y-%m-%d %H:%M:%S"
date +%T
date -u
Format Strings
| Token | Meaning | Example |
|---|
%Y | 4-digit year | 2026 |
%m | Month (01-12) | 04 |
%d | Day (01-31) | 13 |
%H | Hour 24h (00-23) | 14 |
%M | Minute (00-59) | 30 |
%S | Second (00-59) | 45 |
%s | Epoch seconds | 1681400000 |
%N | Nanoseconds (GNU) | 123456789 |
%Z | Timezone name | EDT |
%z | Timezone offset | -0400 |
%A | Day of week name | Monday |
%a | Day abbrev | Mon |
%B | Month name | April |
%j | Day of year | 103 |
%u | Day of week (1=Mon) | 1 |
%F | Shorthand %Y-%m-%d | 2026-04-13 |
macOS vs GNU date -- Critical Differences
macOS ships BSD date. Linux ships GNU date. They are not compatible.
Parse a date string
date -d "2026-04-13 14:30:00" +%s
date -j -f "%Y-%m-%d %H:%M:%S" "2026-04-13 14:30:00" +%s
Date math
date -d "now + 3 days"
date -d "2026-04-13 + 2 hours"
date -d "yesterday"
date -d "last monday"
date -v+3d
date -v-2H
date -v+1m
date -v+30M
date -v-1y
date -v+1w
macOS -v flags: y year, m month, w week, d day, H hour, M minute, S second.
Epoch Conversions
Epoch seconds: 10 digits (e.g., 1681400000). Epoch milliseconds: 13 digits (e.g., 1681400000000). If 13 digits, divide by 1000 first.
date -d @1681400000
date -r 1681400000
date -d @$((1681400000000 / 1000))
date -r $((1681400000000 / 1000))
date -d "2026-04-13T14:30:00" +%s
date -j -f "%Y-%m-%dT%H:%M:%S" "2026-04-13T14:30:00" +%s
date +%s
ISO 8601
date --iso-8601=seconds
date -u +"%Y-%m-%dT%H:%M:%SZ"
date +"%Y-%m-%dT%H:%M:%S%z"
Note: ISO 8601 wants -04:00 not -0400. GNU handles this; on macOS insert the colon yourself.
Timezone Conversion
TZ="America/New_York" date
TZ="Europe/London" date
TZ="Asia/Tokyo" date
TZ="UTC" date
TZ="Asia/Tokyo" date -d "2026-04-13 14:30:00 EDT"
epoch=$(date -j -f "%Y-%m-%d %H:%M:%S" "2026-04-13 14:30:00" +%s)
TZ="Asia/Tokyo" date -r "$epoch"
ls /usr/share/zoneinfo/America/
Compare Two Dates
Convert both to epoch, then compare.
d1=$(date -d "2026-04-13" +%s)
d2=$(date -d "2026-05-01" +%s)
d1=$(date -j -f "%Y-%m-%d" "2026-04-13" +%s)
d2=$(date -j -f "%Y-%m-%d" "2026-05-01" +%s)
if [ "$d1" -lt "$d2" ]; then
echo "d1 is earlier"
fi
diff_days=$(( (d2 - d1) / 86400 ))
echo "$diff_days days apart"
Parse Dates from Strings
date -d "April 13, 2026" +%F
date -d "13 Apr 2026" +%F
date -d "next friday" +%F
date -d "2 weeks ago" +%F
date -j -f "%B %d, %Y" "April 13, 2026" +%F
date -j -f "%d %b %Y" "13 Apr 2026" +%F
Useful One-Liners
How long ago was this timestamp
ts=1681400000
now=$(date +%s)
diff=$((now - ts))
echo "$((diff / 86400)) days, $(( (diff % 86400) / 3600 )) hours ago"
What day of the week was a date
date -d "2026-04-13" +%A
date -j -f "%Y-%m-%d" "2026-04-13" +%A
Seconds between two timestamps
echo $(( $(date -d "2026-05-01" +%s) - $(date -d "2026-04-13" +%s) ))
echo $(( $(date -j -f "%Y-%m-%d" "2026-05-01" +%s) - $(date -j -f "%Y-%m-%d" "2026-04-13" +%s) ))
Relative time description
relative_time() {
local diff=$(( $(date +%s) - $1 ))
if [ $diff -lt 60 ]; then echo "${diff}s ago"
elif [ $diff -lt 3600 ]; then echo "$((diff/60))m ago"
elif [ $diff -lt 86400 ]; then echo "$((diff/3600))h ago"
else echo "$((diff/86400))d ago"
fi
}
relative_time 1681400000
Date in Scripts
Generate timestamped filenames
backup_file="db-backup-$(date +%Y%m%d-%H%M%S).sql.gz"
log_file="deploy-$(date +%F).log"
Log timestamps
log() { echo "[$(date +"%Y-%m-%d %H:%M:%S")] $*"; }
log "Deployment started"
Measure elapsed time
start=$(date +%s)
echo "Took $(( $(date +%s) - start ))s"
When date Is Not Enough
Python
python3 -c "from dateutil.parser import parse; print(parse('April 13, 2026 2:30 PM').isoformat())"
python3 -c "from datetime import datetime; print(datetime.fromtimestamp(1681400000000/1000))"
python3 -c "from datetime import datetime,timezone; print((datetime.now(timezone.utc)-datetime(2026,4,13,tzinfo=timezone.utc)).total_seconds())"
python3 -c "from datetime import datetime; from zoneinfo import ZoneInfo; print(datetime.now(ZoneInfo('Asia/Tokyo')))"
Node
node -e "console.log(new Date(1681400000000).toISOString())"
node -e "console.log(new Date('April 13, 2026').toISOString())"
node -e "console.log(Date.now())"
node -e "console.log((Date.now() - new Date('2026-04-13').getTime()) / 3600000, 'hours')"
Quick Reference
| Task | GNU (Linux) | macOS (BSD) |
|---|
| Current epoch | date +%s | date +%s |
| Epoch to date | date -d @EPOCH | date -r EPOCH |
| Parse date string | date -d "STRING" | date -j -f "FMT" "STRING" |
| Add 3 days | date -d "now + 3 days" | date -v+3d |
| Subtract 2 hours | date -d "now - 2 hours" | date -v-2H |
| ISO 8601 | date --iso-8601=seconds | date -u +"%Y-%m-%dT%H:%M:%SZ" |
| Specific TZ | TZ="Zone" date | TZ="Zone" date |