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 2026Format Codes
A plus sign followed by codes builds a custom format.
| Code | Meaning | Sample |
|---|---|---|
| %Y | Four-digit year | 2026 |
| %m | Month number | 10 |
| %d | Day of month | 16 |
| %H | Hour (24-hour) | 14 |
| %M | Minute | 30 |
| %S | Second | 45 |
| %A | Weekday name | Friday |
| %B | Month name | October |
| %s | Seconds since 1 January 1970 | 1792161045 |
date +%Y-%m-%d
date +"%d/%m/%Y %H:%M"
date +%AOutput:
2026-10-16
16/10/2026 14:30
FridayTimestamp 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" +%AThe 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: 85Built-In printf Time
Bash 4.2 and higher prints time without starting a new process:
printf '%(%Y-%m-%d)T\n' -1This approach runs faster inside long loops.
Measure How Long a Script Runs
Use the time Command
time ./backup.shThe 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 secondTime Zones
TZ="America/New_York" date
TZ="Asia/Tokyo" date +"%H:%M"
date -uThe -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"
fiA 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 finishedPractical 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"
fiThe code %u returns 1 for Monday and 7 for Sunday, which makes weekday checks short and clear.
Key Takeaways
date +FORMATprints custom date text.date -d "phrase"calculates past and future dates on Linux.date +%sgives seconds for easy math.SECONDSandtimemeasure script duration.- Year-month-day order keeps file names sorted.
