Bash Date Time

Scripts often need the current date or time. Backup names, log entries, and reports all depend on them. The date command prints and formats time, and a few extra tricks handle calculations and timers.

Show the Current Date

date
Fri Oct 16 14:30:45 IST 2026

Format Codes

A plus sign followed by codes builds a custom format.

CodeMeaningSample
%YFour-digit year2026
%mMonth number10
%dDay of month16
%HHour (24-hour)14
%MMinute30
%SSecond45
%AWeekday nameFriday
%BMonth nameOctober
%sSeconds since 1 January 19701792161045
date +%Y-%m-%d
date +"%d/%m/%Y %H:%M"
date +%A

Output:

2026-10-16
16/10/2026 14:30
Friday

Timestamp for File Names

stamp=$(date +%Y%m%d_%H%M%S)
cp data.txt "data_$stamp.txt"

The year-first order sorts correctly in file listings.

Calculate Other Dates

GNU date accepts plain English phrases:

date -d "tomorrow" +%Y-%m-%d
date -d "yesterday" +%Y-%m-%d
date -d "7 days ago" +%Y-%m-%d
date -d "next monday" +%Y-%m-%d
date -d "2026-12-25" +%A

The last command reports which weekday Christmas falls on. macOS uses a different syntax: date -v+1d +%Y-%m-%d.

Convert Between Date and Seconds

Seconds make math simple. Convert two dates, subtract, and divide.

#!/bin/bash
start=$(date -d "2026-10-01" +%s)
end=$(date -d "2026-12-25" +%s)
days=$(( (end - start) / 86400 ))
echo "Days between: $days"

Output:

Days between: 85

Built-In printf Time

Bash 4.2 and higher prints time without starting a new process:

printf '%(%Y-%m-%d)T\n' -1

This approach runs faster inside long loops.

Measure How Long a Script Runs

Use the time Command

time ./backup.sh

The output shows real, user, and system time.

Use the SECONDS Variable

#!/bin/bash
SECONDS=0
sleep 3
echo "Took $SECONDS seconds"

Pause and Wait

sleep 5        # five seconds
sleep 2m       # two minutes
sleep 0.5      # half a second

Time Zones

TZ="America/New_York" date
TZ="Asia/Tokyo" date +"%H:%M"
date -u

The -u option shows UTC time. Log files in UTC avoid confusion across regions.

Practical Example: Greeting by Time

#!/bin/bash
hour=$(date +%H)

if [ "$hour" -lt 12 ]; then
  echo "Good morning"
elif [ "$hour" -lt 17 ]; then
  echo "Good afternoon"
else
  echo "Good evening"
fi

A leading zero in values like 08 can confuse arithmetic inside double parentheses, because Bash reads it as an octal number. The -lt test in single brackets handles it safely.

Practical Example: Log Entry With a Timestamp

A small function adds the current time in front of every message. Scripts that run overnight use this pattern to show when each step happened.

#!/bin/bash
note() {
  echo "$(date '+%Y-%m-%d %H:%M:%S') $*"
}

note "Backup started"
sleep 2
note "Backup finished"

Output:

2026-10-16 14:30:45 Backup started
2026-10-16 14:30:47 Backup finished

Practical Example: Countdown to a Date

This script reports how many days remain until a chosen date. Seconds make the subtraction simple, and one division turns the result into days.

#!/bin/bash
target=$(date -d "2027-01-01" +%s)
today=$(date +%s)
left=$(( (target - today) / 86400 ))
echo "Days until the new year: $left"

The same idea works for deadlines, license renewals, and certificate expiry checks.

Check the Day of the Week

day=$(date +%u)
if [ "$day" -ge 6 ]; then
  echo "Weekend: skip the report"
else
  echo "Weekday: run the report"
fi

The code %u returns 1 for Monday and 7 for Sunday, which makes weekday checks short and clear.

Key Takeaways

  • date +FORMAT prints custom date text.
  • date -d "phrase" calculates past and future dates on Linux.
  • date +%s gives seconds for easy math.
  • SECONDS and time measure script duration.
  • Year-month-day order keeps file names sorted.