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

  1. Display current date/time
  2. Format timestamps
  3. Calculate date differences
  4. Log file naming
  5. 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

  1. Add days:

    date -d "+7 days"
    date -d "next week"
  2. Subtract time:

    date -d "-1 month"
    date -d "yesterday"
  3. Specific calculations:

    date -d "2024-01-01 +100 days"

Common Usage Patterns

  1. Timestamp for logs:

    echo "$(date): Process started" >> log.txt
  2. Backup file naming:

    cp file.txt "file_$(date +%Y%m%d_%H%M%S).txt"
  3. Age calculation:

    date -d "1990-01-01" +%s  # Birth timestamp

File Timestamps

  1. Show file modification time:

    date -r filename
  2. Compare file ages:

    if [ $(date -r file1 +%s) -gt $(date -r file2 +%s) ]; then
        echo "file1 is newer"
    fi

Time Zones

  1. UTC time:

    date -u
  2. Specific timezone:

    TZ='America/New_York' date
    TZ='Europe/London' date
  3. 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

  1. Use consistent date formats
  2. Consider timezone implications
  3. Use epoch time for calculations
  4. Validate date inputs
  5. Handle leap years properly

Scripting Applications

  1. Log rotation by date:

    #!/bin/bash
    LOG_DATE=$(date +%Y%m%d)
    mv app.log "app_${LOG_DATE}.log"
  2. Backup automation:

    BACKUP_DIR="/backup/$(date +%Y/%m/%d)"
    mkdir -p "$BACKUP_DIR"
  3. Performance timing:

    START_TIME=$(date +%s)
    # ... operations ...
    END_TIME=$(date +%s)
    DURATION=$((END_TIME - START_TIME))
    echo "Operation took $DURATION seconds"

Date Parsing

  1. Parse various formats:

    date -d "March 15, 2024"
    date -d "15/03/2024"
    date -d "2024-03-15T14:30:00"
  2. Relative dates:

    date -d "next Monday"
    date -d "last Friday"
    date -d "2 weeks ago"

Integration Examples

  1. With find for file operations:

    find /logs -name "*.log" -newermt "$(date -d '7 days ago')"
  2. Cron job scheduling:

    # Run only on weekdays
    if [ $(date +%u) -le 5 ]; then
        run_weekday_job
    fi
  3. System monitoring:

    echo "$(date): CPU usage $(top -bn1 | grep "Cpu(s)" | awk '{print $2}')" >> monitor.log

Epoch Time

  1. Current epoch:

    date +%s
  2. Convert from epoch:

    date -d @1710504645
  3. 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

  1. Log format:

    date "+[%Y-%m-%d %H:%M:%S]"
  2. Filename safe:

    date "+%Y%m%d_%H%M%S"
  3. Human readable:

    date "+%A, %B %d, %Y at %I:%M %p"

Troubleshooting

  1. Timezone confusion
  2. Daylight saving time issues
  3. Leap year calculations
  4. Date format parsing errors
  5. System clock synchronization

Security Considerations

  1. Validate date inputs
  2. Be aware of timezone attacks
  3. Use NTP for time synchronization
  4. Log timestamp integrity
  5. Handle time-based race conditions

Advanced Usage

  1. Week calculations:

    date +%V  # ISO week number
    date +%U  # Week number (Sunday start)
    date +%W  # Week number (Monday start)
  2. Day of year:

    date +%j  # Day of year (001-366)
  3. Quarter calculation:

    MONTH=$(date +%m)
    QUARTER=$(((MONTH - 1) / 3 + 1))
    echo "Q$QUARTER"