How to Convert Timestamp to Date
• 2 min read
bash timestamp epoch date conversion unix time date formatting
Quick Answer: Convert Timestamp to Date in Bash
To convert Unix timestamp to date, use date -d @1645382400 to show the date, or date -d @1645382400 +%Y-%m-%d for specific format. To convert date to timestamp, use date -d "2026-02-21" +%s. Both use the date command with different format specifiers.
Quick Comparison: Timestamp Conversion Methods
| Operation | Command | Output | Use Case |
|---|---|---|---|
| Epoch to date | date -d @1645382400 | Readable date | Log analysis |
| Date to epoch | date -d "2026-02-21" +%s | Timestamp | Calculations |
| Custom format | date -d @ts +%Y-%m-%d | Specific format | Reporting |
| Current time | date +%s | Now timestamp | Scripts |
Bottom line: Use date -d @timestamp to read; use date -d "date" +%s to convert to timestamp.
Convert Unix timestamps (epoch time) to readable date formats and vice versa. Learn converting between timestamps and date strings.
Method 1: Convert Epoch to Date
# Basic conversion
date -d @1645382400
# Output: Sat Feb 21 00:00:00 UTC 2026
# Specific format
date -d @1645382400 +%Y-%m-%d
# Output: 2026-02-21
Format Options for timestamp
timestamp=1645382400
# Date only
date -d @$timestamp +%Y-%m-%d
# Time only
date -d @$timestamp +%H:%M:%S
# Full datetime
date -d @$timestamp '+%Y-%m-%d %H:%M:%S'
# Day of week
date -d @$timestamp +%A
# Custom format
date -d @$timestamp '+%A, %B %d, %Y at %I:%M %p'
Function to Convert Timestamp
#!/bin/bash
epoch_to_date() {
local timestamp="$1"
local format="${2:-%Y-%m-%d %H:%M:%S}"
if [ -z "$timestamp" ]; then
echo "Usage: epoch_to_date <timestamp> [format]"
return 1
fi
date -d @$timestamp +"$format"
}
# Usage
epoch_to_date 1645382400
epoch_to_date 1645382400 '%Y-%m-%d'
epoch_to_date 1645382400 '%A, %B %d, %Y'
Output:
2026-02-21 00:00:00
2026-02-21
Saturday, February 21, 2026
Convert Date to Epoch
# Convert date string to epoch
date -d "2026-02-21" +%s
# Output: 1645382400
# Convert datetime to epoch
date -d "2026-02-21 14:30:00" +%s
# Output: 1645429800
Function for Date to Epoch
#!/bin/bash
date_to_epoch() {
local date="$1"
if [ -z "$date" ]; then
echo "Usage: date_to_epoch <date>"
return 1
fi
date -d "$date" +%s
}
# Usage
epoch=$(date_to_epoch "2026-02-21")
echo "Epoch: $epoch"
Batch Conversion
#!/bin/bash
# Convert list of timestamps to dates
input_file="$1"
if [ ! -f "$input_file" ]; then
echo "Usage: $0 <timestamps_file>"
exit 1
fi
while read timestamp; do
# Skip empty lines
[ -z "$timestamp" ] && continue
# Convert and display
date_str=$(date -d @$timestamp '+%Y-%m-%d %H:%M:%S')
echo "$timestamp → $date_str"
done < "$input_file"
Test file (timestamps.txt):
1645382400
1645429800
1645516200
Output:
1645382400 → 2026-02-21 00:00:00
1645429800 → 2026-02-21 14:30:00
1645516200 → 2026-02-22 14:30:00
Practical Example: Log Timestamp Conversion
#!/bin/bash
# File: convert_log_timestamps.sh
log_file="$1"
output_file="${2:-converted.log}"
if [ ! -f "$log_file" ]; then
echo "Usage: $0 <logfile> [output_file]"
exit 1
fi
echo "Converting timestamps in $log_file..."
> "$output_file"
while IFS= read -r line; do
# Check if line starts with timestamp
if [[ $line =~ ^([0-9]{10})(.*)$ ]]; then
timestamp="${BASH_REMATCH[1]}"
rest="${BASH_REMATCH[2]}"
# Convert timestamp
readable=$(date -d @$timestamp '+%Y-%m-%d %H:%M:%S')
echo "$readable$rest" >> "$output_file"
else
echo "$line" >> "$output_file"
fi
done < "$log_file"
echo "✓ Converted log saved to: $output_file"
Format Variations
timestamp=1645382400
# Different formats
echo "ISO 8601: $(date -d @$timestamp --iso-8601=seconds)"
echo "RFC 2822: $(date -d @$timestamp --rfc-2822)"
echo "Compact: $(date -d @$timestamp +%Y%m%d_%H%M%S)"
echo "Time only: $(date -d @$timestamp +%H:%M:%S)"
echo "Day name: $(date -d @$timestamp +%A)"
echo "Unix format: $(date -d @$timestamp)"
Output:
ISO 8601: 2026-02-21T00:00:00+00:00
RFC 2822: Sat, 21 Feb 2026 00:00:00 +0000
Compact: 20260221_000000
Time only: 00:00:00
Day name: Saturday
Unix format: Sat Feb 21 00:00:00 UTC 2026
Round-trip Conversion
#!/bin/bash
# Convert date → epoch → date
original_date="2026-02-21 14:30:00"
# Convert to epoch
epoch=$(date -d "$original_date" +%s)
echo "Original: $original_date"
echo "Epoch: $epoch"
# Convert back to date
back_to_date=$(date -d @$epoch '+%Y-%m-%d %H:%M:%S')
echo "Converted back: $back_to_date"
Output:
Original: 2026-02-21 14:30:00
Epoch: 1645429800
Converted back: 2026-02-21 14:30:00
Timezone Handling
#!/bin/bash
timestamp=1645382400
timezone="${1:-UTC}"
# Convert with specific timezone
TZ=$timezone date -d @$timestamp
# Examples
echo "UTC: $(TZ=UTC date -d @$timestamp '+%Y-%m-%d %H:%M:%S')"
echo "EST: $(TZ=America/New_York date -d @$timestamp '+%Y-%m-%d %H:%M:%S')"
echo "IST: $(TZ=Asia/Kolkata date -d @$timestamp '+%Y-%m-%d %H:%M:%S')"
Practical Example: File Timestamp Interpretation
#!/bin/bash
# Convert file modification time from epoch
file="$1"
if [ ! -f "$file" ]; then
echo "Usage: $0 <file>"
exit 1
fi
# Get modification time as epoch
mod_epoch=$(stat -c %Y "$file" 2>/dev/null || stat -f %m "$file" 2>/dev/null)
# Convert to readable format
mod_date=$(date -d @$mod_epoch '+%Y-%m-%d %H:%M:%S')
echo "File: $file"
echo "Modified: $mod_date"
echo "Epoch: $mod_epoch"
Common Formats Table
| Format | Example | Code |
|---|---|---|
| ISO 8601 | 2026-02-21T00:00:00+00:00 | --iso-8601 |
| RFC 2822 | Sat, 21 Feb 2026 | --rfc-2822 |
| Human | Saturday, February 21, 2026 | +%A, %B %d, %Y |
| Short | 02/21/2026 | +%m/%d/%Y |
| Compact | 20260221 | +%Y%m%d |
Common Mistakes
- Missing @ symbol - must use
@before epoch:@1645382400 - Wrong format codes - use lowercase
%dfor day - Timezone not set - epoch is always UTC, use TZ variable to convert
- Not quoting timestamp - can fail with spaces
- Using system date instead of -d - need
-dflag for conversions
Performance Tips
- Cache conversion results if used repeatedly
- Use
awkwith strftime for bulk conversions - Avoid looping date command for large files
Key Points
- Use
date -d @$timestampfor epoch conversion - Use
date -d "$date" +%sfor date to epoch - Include @ before timestamp number
- Use + format codes for custom output
- Remember epoch is always UTC
Summary
Converting between timestamps and dates is essential for log analysis and scripting. The date command makes it simple with the @ prefix for epochs and +%s for conversion to epoch.