Logs say 1704672000. Brain says “when was that?!” Here’s how to translate epoch time to actual human time.
Convert Unix timestamp to local time#
1
2
3
4
5
6
7
8
9
10
| # Using date command
date -d @1704672000
# Output: Sun Jan 7 20:10:00 IST 2024
# Custom format
date -d @1704672000 '+%Y-%m-%d %H:%M:%S'
# Output: 2024-01-07 20:10:00
# With timezone
TZ='America/New_York' date -d @1704672000
|
Convert local time to Unix timestamp#
1
2
3
4
5
6
7
8
9
10
11
| # Current time
date +%s
# Output: 1704672000
# Specific date to timestamp
date -d "2024-01-07 20:10:00" +%s
# Output: 1704672000
# Different date formats
date -d "Jan 7 2024" +%s
date -d "2024-01-07T20:10:00" +%s
|
Bulk conversion (from file or variable)#
1
2
3
4
5
6
7
8
9
| # Convert multiple timestamps
echo "1704672000" | while read ts; do
date -d @$ts '+%Y-%m-%d %H:%M:%S'
done
# From file with timestamps
cat timestamps.txt | while read ts; do
echo "Timestamp: $ts -> $(date -d @$ts)"
done
|
macOS/BSD date syntax#
macOS uses different syntax:
1
2
3
4
5
| # Unix timestamp to local time
date -r 1704672000
# Local time to Unix timestamp
date -j -f "%Y-%m-%d %H:%M:%S" "2024-01-07 20:10:00" +%s
|
Millisecond timestamps#
1
2
3
4
5
6
| # Convert millisecond timestamp (divide by 1000)
ts_ms=1704672000000
date -d @$((ts_ms / 1000))
# Get current time in milliseconds
date +%s%3N
|
Quick one-liner for logs#
1
2
| # Replace timestamps in log file
awk '{cmd="date -d @"$1; cmd | getline d; close(cmd); $1=d; print}' logfile.txt
|