About date
date displays or sets the system date and time. It's essential for logging, timestamps, scheduling, and date calculations.
Basic Syntax: date [OPTION]... [+FORMAT]
Key Format Specifiers: %Y (year), %m (month), %d (day), %H (hour), %M (minute), %S (second), %A (weekday), %B (month name)
Example 1: Basic Date Display
Display current date and time in various default formats.
# Default format
date
# ISO 8601 format
date -I
date --iso-8601
# RFC 5322 format (email headers)
date -R
# UTC time
date -u
date --utcOutput:
Tue Nov 12 15:36:31 EST 2025
2025-11-12
Tue, 12 Nov 2025 15:36:31 -0500
Tue Nov 12 20:36:31 UTC 2025Example 2: Custom Date Formatting
Create custom date formats using format specifiers.
# Standard formats
date '+%Y-%m-%d'
date '+%H:%M:%S'
date '+%Y-%m-%d %H:%M:%S'
# Verbose formats
date '+%A, %B %d, %Y'
date '+%I:%M %p on %A'
# Compact formats
date '+%Y%m%d'
date '+%Y%m%d_%H%M%S'
# Custom delimiters
date '+%d/%m/%Y'
date '+%m-%d-%Y'Output:
2025-11-12
15:36:31
2025-11-12 15:36:31
Tuesday, November 12, 2025
03:36 PM on Tuesday
20251112
20251112_153631Note:
The + sign is required before format strings. Combine multiple specifiers to create any date format needed.Example 3: Timestamps for Files and Logs
Generate timestamps perfect for filenames and logging.
# Create timestamped backup
tar -czf backup_$(date +%Y%m%d).tar.gz /data/
# Log with timestamp
echo "$(date '+%Y-%m-%d %H:%M:%S') - Backup completed" >> backup.log
# Timestamped filename
touch report_$(date +%Y-%m-%d_%H-%M-%S).txt
# ISO 8601 with seconds
date '+%Y-%m-%dT%H:%M:%S'
# Unix epoch timestamp
date +%s
# Epoch with milliseconds
date +%s%3NExamples:
backup_20251112.tar.gz
2025-11-12 15:36:31 - Backup completed
report_2025-11-12_15-36-31.txt
2025-11-12T15:36:31
1731441391
1731441391123Example 4: Date Arithmetic
Calculate dates in the past or future.
# Tomorrow
date -d "tomorrow"
date -d "+1 day"
# Yesterday
date -d "yesterday"
date -d "-1 day"
# Next week
date -d "+1 week"
date -d "+7 days"
# 30 days ago
date -d "-30 days" '+%Y-%m-%d'
# Specific date operations
date -d "2025-01-01 +90 days"
# Next month
date -d "+1 month"
# Complex calculations
date -d "2025-11-12 +2 weeks +3 days"Output:
Wed Nov 13 15:36:31 EST 2025
Mon Nov 11 15:36:31 EST 2025
Tue Nov 19 15:36:31 EST 2025
2025-10-13
Wed Apr 01 00:00:00 EDT 2025Note:
Use -d or --date for relative dates. Supports days, weeks, months, years, hours, minutes, seconds.Example 5: Parsing and Converting Dates
Convert between different date formats and parse date strings.
# Parse specific date
date -d "2025-11-12"
date -d "Nov 12, 2025"
date -d "11/12/2025"
# Parse with time
date -d "2025-11-12 15:30:00"
# Convert to different format
date -d "2025-11-12" '+%A, %B %d, %Y'
# Parse epoch timestamp
date -d @1731441391
# Convert epoch to readable
date -d @1731441391 '+%Y-%m-%d %H:%M:%S'
# ISO to RFC format
date -d "2025-11-12T15:30:00" -ROutput:
Tue Nov 12 00:00:00 EST 2025
Tuesday, November 12, 2025
Tue Nov 12 15:36:31 EST 2025
2025-11-12 15:36:31Example 6: Working with Different Time Zones
Display times in different time zones using TZ environment variable.
# UTC time
TZ=UTC date
# Different time zones
TZ=America/New_York date
TZ=America/Los_Angeles date
TZ=Europe/London date
TZ=Asia/Tokyo date
# Show multiple zones
for tz in UTC America/New_York Europe/London Asia/Tokyo; do
echo "$tz: $(TZ=$tz date '+%H:%M')"
done
# Convert specific time to different zone
TZ=Asia/Tokyo date -d "TZ=\"America/New_York\" 2025-11-12 15:00"Output:
Tue Nov 12 20:36:31 UTC 2025
Tue Nov 12 15:36:31 EST 2025
Tue Nov 12 12:36:31 PST 2025
Tue Nov 12 20:36:31 GMT 2025
Wed Nov 13 05:36:31 JST 2025Example 7: Day of Week and Date Components
Extract specific date components and calculate day of week.
# Day of week
date '+%A'
date '+%a'
date '+%u' # 1-7 (Monday=1)
date '+%w' # 0-6 (Sunday=0)
# Month names
date '+%B' # Full month name
date '+%b' # Abbreviated
# Various components
date '+%Y' # Year
date '+%m' # Month (01-12)
date '+%d' # Day (01-31)
date '+%j' # Day of year (001-366)
date '+%V' # Week number (ISO)
# Find day of specific date
date -d "2025-12-25" '+%A'
# Check if date is weekend
day=$(date '+%u')
[ $day -gt 5 ] && echo "Weekend" || echo "Weekday"Output:
Tuesday
Tue
2
2
November
Nov
ThursdayExample 8: Calculating Time Differences
Calculate elapsed time and differences between dates.
# Calculate days between dates
start=$(date -d "2025-01-01" +%s)
end=$(date -d "2025-11-12" +%s)
diff=$((($end - $start) / 86400))
echo "$diff days"
# Time since epoch
date +%s
# Days until specific date
target=$(date -d "2025-12-31" +%s)
now=$(date +%s)
days_until=$(( ($target - $now) / 86400 ))
echo "$days_until days until end of year"
# Time script execution
start_time=$(date +%s)
# ... commands here ...
end_time=$(date +%s)
echo "Elapsed: $(($end_time - $start_time)) seconds"
# Age calculation
birthdate=$(date -d "1990-01-01" +%s)
now=$(date +%s)
age=$(( ($now - $birthdate) / 31536000 ))
echo "$age years old"Output:
316 days
49 days until end of year
Elapsed: 45 seconds
35 years oldExample 9: Scheduling and Cron-Related Uses
Use date for scheduling, cron jobs, and periodic tasks.
# First day of month check
if [ $(date +%d) -eq 1 ]; then
echo "First day of month - run monthly tasks"
fi
# First Monday of month
if [ $(date +%d) -le 7 ] && [ $(date +%u) -eq 1 ]; then
echo "First Monday"
fi
# Business hours check
hour=$(date +%H)
if [ $hour -ge 9 ] && [ $hour -lt 17 ]; then
echo "Business hours"
fi
# Weekend check
[ $(date +%u) -gt 5 ] && echo "Weekend mode" || echo "Weekday mode"
# Quarter check
month=$(date +%m)
case $month in
01|02|03) quarter=Q1 ;;
04|05|06) quarter=Q2 ;;
07|08|09) quarter=Q3 ;;
10|11|12) quarter=Q4 ;;
esac
echo "Current quarter: $quarter"Example 10: Practical Scripting Examples
Real-world date usage in scripts and automation.
# Rotating log files
log_file="app_$(date +%Y%m%d).log"
echo "$(date '+%Y-%m-%d %H:%M:%S') - Application started" >> "$log_file"
# Archive old files
find /logs -name "*.log" -mtime +30 -exec \
mv {} archive/archive_$(date +%Y%m%d)_{} \;
# Daily backup script
backup_dir="/backup/$(date +%Y/%m/%d)"
mkdir -p "$backup_dir"
tar -czf "$backup_dir/backup_$(date +%H%M%S).tar.gz" /data/
# Report generation
cat << EOF > report.txt
System Report
Generated: $(date '+%A, %B %d, %Y at %I:%M %p')
Uptime: $(uptime -p)
EOF
# Expiration check
expiry_date="2025-12-31"
expiry_epoch=$(date -d "$expiry_date" +%s)
now_epoch=$(date +%s)
if [ $now_epoch -gt $expiry_epoch ]; then
echo "License expired"
else
days_left=$(( ($expiry_epoch - $now_epoch) / 86400 ))
echo "License valid for $days_left days"
fiAdditional Tips & Best Practices
- ISO 8601: Use
date -Ifor sortable dates. Add time withdate -Iseconds - Epoch time: Unix timestamp (
date +%s) is perfect for calculations and databases - Portability: GNU date (-d option) differs from BSD/macOS date. Check with
date --version - File modification time: Use
date -r filenameto show file's modification time - Set system time: Requires root:
sudo date -s "2025-11-12 15:30:00"(use NTP instead) - Hardware clock: Use
hwclockto sync hardware clock with system time - NTP sync: Prefer
ntpdateortimedatectlover manual date setting - Locale: Date format affected by LC_TIME. Set with
LANG=en_US.UTF-8 date - Filename safety: Avoid spaces in date formats for filenames. Use underscores or hyphens
- Leap years: date handles leap years, daylight saving, and timezone changes automatically
- Performance: Cache epoch time in loops:
now=$(date +%s)then reuse $now - Debugging: Use
date -dto test date parsing before using in scripts