Current date and time
date
Custom format
date +"%Y-%m-%d"
date +"%Y-%m-%d %H:%M:%S"
date +"%d/%m/%Y"
date +"%B %d, %Y"
Common format codes
%Y
%y
%m
%B
%b
%d
%A
%a
%H
%I
%M
%S
%p
%s
%Z
Unix timestamp
date +%s
date -d @1699660800
date -r 1699660800
Date arithmetic
date -d "tomorrow"
date -d "+1 day"
date -d "yesterday"
date -d "-1 day"
date -d "+1 week"
date -d "-1 month"
date -d "2025-12-25"
date -d "+30 days"
Time calculations
date -d "2 hours ago"
date -d "+30 minutes"
date -d "next Monday"
date -d "last Friday"
Set system date (requires root)
sudo date -s "2025-11-11 14:30:00"
ISO 8601 format
date -I
date -Iseconds
date --iso-8601=seconds
RFC format
date -R
date --rfc-3339=seconds
Compare dates
#!/bin/bash
date1="2025-01-01"
date2="2025-12-31"
timestamp1=$(date -d "$date1" +%s)
timestamp2=$(date -d "$date2" +%s)
if [ $timestamp1 -lt $timestamp2 ]; then
echo "$date1 is before $date2"
fi
Calculate days between dates
#!/bin/bash
date1="2025-01-01"
date2="2025-12-31"
days=$(( ($(date -d "$date2" +%s) - $(date -d "$date1" +%s)) / 86400 ))
echo "Days between: $days"
File timestamps
stat -c %y file.txt
stat -c %Y file.txt
touch -t 202511111430 file.txt
touch -d "2025-11-11 14:30" file.txt
Timezones
date +%Z
TZ="America/New_York" date
TZ="Europe/London" date
TZ="Asia/Tokyo" date
timedatectl list-timezones
Set timezone
export TZ="America/New_York"
sudo timedatectl set-timezone America/New_York
sudo ln -sf /usr/share/zoneinfo/America/New_York /etc/localtime
NTP sync
timedatectl status
sudo timedatectl set-ntp true
sudo ntpdate pool.ntp.org
sudo chronyd -q
Week number
date +%V
date +%U
date +%W
Day of year
date +%j
Relative dates
date -d "$(date +%Y-%m-01)"
date -d "$(date +%Y-%m-01) +1 month -1 day"
date -d "$(date +%Y-01-01)"
Filename timestamps
cp file.txt file-$(date +%Y%m%d-%H%M%S).txt
echo "Log entry" >> log-$(date +%Y-%m-%d).log
mkdir backup-$(date +%Y%m%d)
Sleep with duration
sleep 5
sleep 2m
sleep 1h
sleep 1d
Countdown timer
#!/bin/bash
seconds=10
while [ $seconds -gt 0 ]; do
echo -ne "$seconds\r"
sleep 1
((seconds--))
done
echo "Time's up!"
Stopwatch
#!/bin/bash
start=$(date +%s)
read -p "Press Enter to stop"
end=$(date +%s)
elapsed=$((end - start))
echo "Elapsed: $elapsed seconds"
Age of file
#!/bin/bash
file="test.txt"
file_time=$(stat -c %Y "$file")
current_time=$(date +%s)
age=$((current_time - file_time))
age_days=$((age / 86400))
echo "File is $age_days days old"
Cron-style date
while true; do
if [ "$(date +%H:%M)" = "02:00" ]; then
./backup.sh
sleep 60
fi
sleep 30
done
Parse log timestamps
grep "ERROR" app.log | cut -d' ' -f1-2
awk '/2025-11-10/,/2025-11-11/' app.log
Calendar
cal
cal 11 2025
cal 2025
cal -3
macOS specific
date -v +1d
date -v -1d
date -v +1m
date -j -f "%Y-%m-%d" "2025-11-11" "+%s"
Benchmark script
#!/bin/bash
start=$(date +%s.%N)
sleep 2
end=$(date +%s.%N)
elapsed=$(echo "$end - $start" | bc)
echo "Execution time: $elapsed seconds"
Pretty uptime
uptime -p
Convert between formats
date -d "2025-11-11T14:30:00" +%s
date -d @1699660800 -Iseconds
date -d "Nov 11, 2025" +%Y-%m-%d