How to Get Current Time in Bash
Quick Answer: Get Current Time in Bash
Use date +%H:%M:%S to display the current time in 24-hour format (HH:MM:SS). For Unix timestamp, use date +%s. Both are built-in date command features that work across all systems.
Quick Comparison: Time Display Methods
| Method | Output | Best For | Notes |
|---|---|---|---|
| date +%H:%M:%S | HH:MM:SS format | Logs and display | 24-hour format, human-readable |
| date +%I:%M:%S %p | HH:MM:SS AM/PM | User-facing output | 12-hour format with AM/PM |
| date +%s | Unix timestamp | Calculations | Seconds since epoch, universal |
| date +%T | HH:MM:SS | Quick display | Shorthand for %H:%M:%S |
Bottom line: Use date +%H:%M:%S for logs, date +%s for comparisons, and date +%I:%M:%S %p when displaying time to users.
Method 1: Standard Time Display
The quickest way to see the current time with date. Without any formatting options, you get the full date and time including timezone:
date
# Output: Sat Feb 21 12:30:45 UTC 2026
When to Use Standard Display
- You need to see the time quickly in the terminal
- Timezone information is important to know
- You’re debugging timestamp-related issues
- You want the full context (day name and timezone)
Method 2: Formatted Time Output
Format the time exactly as you need it using format codes. The plus sign tells date you’re providing a custom format:
date +%H:%M:%S
# Output: 12:30:45
date +"%I:%M:%S %p"
# Output: 02:30:45 PM
date +%H%M%S
# Output: 143045
When to Use Formatted Output
- You’re creating log file entries
- You need the time in a specific format for parsing
- You’re constructing timestamps for filenames
- You want consistent formatting across scripts
- You don’t need timezone information in the output
Time Format Codes
| Code | Meaning |
|---|---|
%H | Hour (00-23) 24-hour format |
%I | Hour (01-12) 12-hour format |
%M | Minute (00-59) |
%S | Second (00-59) |
%T | Time (HH:MM:SS) shorthand |
%p | AM/PM indicator |
%Z | Timezone name |
%3N | Milliseconds |
Method 3: Unix Timestamp
Get the number of seconds since the Unix epoch (January 1, 1970). This is essential for time calculations and comparisons:
date +%s
# Output: 1740148245
# With milliseconds
date +%s%3N
# Output: 1740148245123
When to Use Unix Timestamp
- You need to compare two times mathematically
- You’re storing timestamps in a database
- You need timezone-independent time measurement
- You’re calculating how much time has elapsed
- You want a compact, universal time format
Method 4: Combined Date and Time
Include both date and time in a single formatted output. Perfect for logging:
date +%Y-%m-%d\ %H:%M:%S
# Output: 2026-02-21 14:30:45
# Or without the backslash
date "+%Y-%m-%d %H:%M:%S"
# Output: 2026-02-21 14:30:45
When to Use Combined Format
- You’re creating log file entries
- You need searchable timestamps in logs
- You want human-readable timestamps with dates
- You’re creating audit trails
Storing Time in Variables
Capture the time into variables for repeated use in scripts:
current_time=$(date +%H:%M:%S)
echo "Time: $current_time"
timestamp=$(date +%s)
echo "Unix time: $timestamp"
When to Use Variable Storage
- You need the time value multiple times
- You’re constructing log messages
- You want to avoid calling
daterepeatedly - You’re creating timestamped backups
Time Math
Calculate time offsets using relative date syntax (GNU systems):
# Get time 1 hour ago
date -d "1 hour ago" +%H:%M:%S
# Get time in 30 minutes
date -d "+30 minutes" +%H:%M:%S
Timezone Handling
Work with different timezones when needed:
# Current timezone
date +%Z
# UTC time
date -u +%H:%M:%S
# Specific timezone (GNU)
TZ=America/New_York date +%H:%M:%S
Practical Examples
Log with Timestamp
Create logs with automatic timestamps:
#!/bin/bash
logfile="/var/log/myapp.log"
log_message() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*" >> "$logfile"
}
log_message "Application started"
log_message "Processing complete"
Performance
When you need repeated timestamps in loops, capture once and use:
# Fast way for multiple timestamps
while true; do
timestamp=$(date +%s)
# Use timestamp...
done
Quick Reference
# Basic usage
date # Full date and time
# Common formats
date +%H:%M:%S # 24-hour format
date +%I:%M:%S\ %p # 12-hour format with AM/PM
date +%T # Shorthand for %H:%M:%S
date +%H%M%S # Compact format
# Unix timestamp
date +%s # Seconds since epoch
# Combined with date
date "+%Y-%m-%d %H:%M:%S" # Full datetime
# UTC time
date -u +%H:%M:%S # UTC time only
Summary
Use date +%H:%M:%S for standard time logging, date +%s for timezone-independent calculations, and date +%I:%M:%S %p when displaying time to users. The date command is essential for creating timestamped logs, monitoring operations, and any task requiring current time information.