date
Overview
The date
command displays or sets the system date and time. It’s essential for timestamping, scheduling, and time-based operations in scripts and system administration.
Syntax
date [options] [+format]
date [options] [MMDDhhmm[[CC]YY][.ss]]
Common Options
Option | Description |
---|---|
-d string |
Display time described by string |
-f file |
Process dates from file |
-r file |
Display file’s last modification time |
-s string |
Set system date/time |
-u |
Display/set UTC time |
--iso-8601 |
ISO 8601 format |
--rfc-3339 |
RFC 3339 format |
Format Specifiers
Format | Description | Example |
---|---|---|
%Y |
Year (4 digits) | 2024 |
%y |
Year (2 digits) | 24 |
%m |
Month (01-12) | 03 |
%B |
Month name | March |
%b |
Month abbreviation | Mar |
%d |
Day of month | 15 |
%A |
Day name | Monday |
%a |
Day abbreviation | Mon |
%H |
Hour (00-23) | 14 |
%I |
Hour (01-12) | 02 |
%M |
Minute | 30 |
%S |
Second | 45 |
%p |
AM/PM | PM |
%Z |
Timezone | EST |
%s |
Seconds since epoch | 1710504645 |
Key Use Cases
- Display current date/time
- Format timestamps
- Calculate date differences
- Log file naming
- Script timing
Examples with Explanations
Example 1: Current Date and Time
date
Output: Mon Mar 15 14:30:45 EST 2024
Example 2: Custom Format
date "+%Y-%m-%d %H:%M:%S"
Output: 2024-03-15 14:30:45
Example 3: ISO Format
date --iso-8601
Output: 2024-03-15
Example 4: Specific Date
date -d "2024-12-25"
Output: Wed Dec 25 00:00:00 EST 2024
Date Arithmetic
Add days:
date -d "+7 days" date -d "next week"
Subtract time:
date -d "-1 month" date -d "yesterday"
Specific calculations:
date -d "2024-01-01 +100 days"
Common Usage Patterns
Timestamp for logs:
echo "$(date): Process started" >> log.txt
Backup file naming:
cp file.txt "file_$(date +%Y%m%d_%H%M%S).txt"
Age calculation:
date -d "1990-01-01" +%s # Birth timestamp
File Timestamps
Show file modification time:
date -r filename
Compare file ages:
if [ $(date -r file1 +%s) -gt $(date -r file2 +%s) ]; then echo "file1 is newer" fi
Time Zones
UTC time:
date -u
Specific timezone:
TZ='America/New_York' date TZ='Europe/London' date
Convert timezone:
date -d "2024-03-15 14:30:00 UTC" "+%Y-%m-%d %H:%M:%S %Z"
Performance Analysis
- Very fast operation
- Minimal system resources
- Good for frequent calls
- Efficient timestamp generation
- Low overhead
Best Practices
- Use consistent date formats
- Consider timezone implications
- Use epoch time for calculations
- Validate date inputs
- Handle leap years properly
Scripting Applications
Log rotation by date:
#!/bin/bash LOG_DATE=$(date +%Y%m%d) mv app.log "app_${LOG_DATE}.log"
Backup automation:
BACKUP_DIR="/backup/$(date +%Y/%m/%d)" mkdir -p "$BACKUP_DIR"
Performance timing:
START_TIME=$(date +%s) # ... operations ... END_TIME=$(date +%s) DURATION=$((END_TIME - START_TIME)) echo "Operation took $DURATION seconds"
Date Parsing
Parse various formats:
date -d "March 15, 2024" date -d "15/03/2024" date -d "2024-03-15T14:30:00"
Relative dates:
date -d "next Monday" date -d "last Friday" date -d "2 weeks ago"
Integration Examples
With find for file operations:
find /logs -name "*.log" -newermt "$(date -d '7 days ago')"
Cron job scheduling:
# Run only on weekdays if [ $(date +%u) -le 5 ]; then run_weekday_job fi
System monitoring:
echo "$(date): CPU usage $(top -bn1 | grep "Cpu(s)" | awk '{print $2}')" >> monitor.log
Epoch Time
Current epoch:
date +%s
Convert from epoch:
date -d @1710504645
Date difference in seconds:
START=$(date -d "2024-01-01" +%s) END=$(date -d "2024-12-31" +%s) DIFF=$((END - START)) DAYS=$((DIFF / 86400))
Formatting Examples
Log format:
date "+[%Y-%m-%d %H:%M:%S]"
Filename safe:
date "+%Y%m%d_%H%M%S"
Human readable:
date "+%A, %B %d, %Y at %I:%M %p"
Troubleshooting
- Timezone confusion
- Daylight saving time issues
- Leap year calculations
- Date format parsing errors
- System clock synchronization
Security Considerations
- Validate date inputs
- Be aware of timezone attacks
- Use NTP for time synchronization
- Log timestamp integrity
- Handle time-based race conditions
Advanced Usage
Week calculations:
date +%V # ISO week number date +%U # Week number (Sunday start) date +%W # Week number (Monday start)
Day of year:
date +%j # Day of year (001-366)
Quarter calculation:
MONTH=$(date +%m) QUARTER=$(((MONTH - 1) / 3 + 1)) echo "Q$QUARTER"