Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

  1. Home
  2. Bash Dictionary
  3. date / cal

date / cal

date is a command for retrieving and formatting the current date and time. cal displays a calendar. Both are essential for generating log file names and naming backups in shell scripts.

Syntax

Display the current date and time.

date

Display with a format string.

date +format_string

Assign a formatted date to a variable.

today=$(date +%Y-%m-%d)

Display a specific date and time (GNU date only).

date -d "datetime_string"

Calculate a date N days in the past or future (GNU date only).

date -d "7 days ago" +%Y-%m-%d

Display this month's calendar.

cal
     March 2026
Su Mo Tu We Th Fr Sa
 1  2  3  4  5  6  7
 8  9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31

Specifying a year displays a full year calendar.

cal 2026

Output of cal 2026

Specifying a month and year displays that month's calendar.

cal 3 2026
     March 2026
Su Mo Tu We Th Fr Sa
 1  2  3  4  5  6  7
 8  9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31

Format String Reference

FormatDescription / Output Example
%YFour-digit year (e.g., 2026).
%mMonth (01–12).
%dDay of the month (01–31).
%HHour in 24-hour format (00–23).
%MMinute (00–59).
%SSecond (00–59).
%AFull weekday name in English (e.g., Friday).
%aAbbreviated weekday name in English (e.g., Fri).
%BFull month name in English (e.g., March).
%sUnix timestamp (seconds since the epoch).
%ZTimezone name (e.g., JST).
%Y%m%dYYYYMMDD format (e.g., 20260306).
%Y-%m-%d %H:%M:%SISO 8601-style format (e.g., 2026-03-06 12:00:00).

Sample Code

Display the current date and time.

date
Fri Mar  6 12:00:00 JST 2026

Display the date and datetime using format strings.

echo "Today: $(date +%Y-%m-%d)"
Today: 2026-03-06
echo "Now: $(date '+%Y-%m-%d %H:%M:%S')"
Now: 2026-03-06 12:00:00

Written as a shell script, it looks like this.

date_sample.sh
echo "Today: $(date +%Y-%m-%d)"
echo "Now: $(date '+%Y-%m-%d %H:%M:%S')"
bash date_sample.sh
Today: 2026-03-06
Now: 2026-03-06 12:00:00

Use a date in a backup file name.

backup.sh
today=$(date +%Y%m%d)
tar -czf "backup_${today}.tar.gz" /var/www/html/
echo "Backup complete: backup_${today}.tar.gz"
bash backup.sh
Backup complete: backup_20260306.tar.gz

Define a function that prepends a timestamp to log messages. The following is written as a shell script.

log_sample.sh
log() {
    echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*"
}
log "Process started"
log "Data retrieval complete"
bash log_sample.sh
[2026-03-06 12:00:00] Process started
[2026-03-06 12:00:03] Data retrieval complete

You can also enter a function definition directly in the terminal. After pressing Enter following the {, a > prompt appears — this means input is still expected. Enter } to complete the definition.

log() {
    echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*"
}
log "Process started"
[2026-03-06 12:00:00] Process started
log "Data retrieval complete"
[2026-03-06 12:00:03] Data retrieval complete

Measure elapsed time using Unix timestamps. The following is written as a shell script.

timer.sh
start=$(date +%s)
sleep 2
end=$(date +%s)
echo "Elapsed time: $((end - start)) seconds"
bash timer.sh
Elapsed time: 2 seconds

Calculate relative dates with GNU date (Linux). The following is written as a shell script.

date_calc.sh
# Date 7 days from now (Linux)
date -d "7 days" +%Y-%m-%d

# Date 7 days ago (Linux)
date -d "7 days ago" +%Y-%m-%d
bash date_calc.sh
2026-03-13
2026-02-27

On macOS (BSD date), use the -v option instead.

date_calc.sh
# Date 7 days from now (macOS)
date -v+7d +%Y-%m-%d

# Date 7 days ago (macOS)
date -v-7d +%Y-%m-%d
bash date_calc.sh
2026-03-13
2026-02-27

Display a calendar.

cal 3 2026
     March 2026
Su Mo Tu We Th Fr Sa
 1  2  3  4  5  6  7
 8  9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31

Notes

For naming backups in scripts, $(date +%Y%m%d) and $(date +%Y%m%d_%H%M%S) are the standard conventions. For log output, wrapping $(date '+%Y-%m-%d %H:%M:%S') in a function keeps your code clean and reusable.

Relative date calculation with date -d is a GNU date (Linux) feature. On macOS (BSD date), write it as date -v+7d instead. For portable scripts, you can also use python3 -c "from datetime import..." as an alternative.

If you find any errors or copyright issues, please .