When you SSH into a Linux server and need to check a timestamp, debug a cron job, or figure out why a log entry is off by a few hours, you reach for the command line. Linux has a rich set of built-in tools for working with Unix timestamps — no browser, no GUI, no dependencies required.

This guide covers every command-line tool you need for Unix timestamp conversion on Linux, from the basic date command to more advanced utilities like timedatectl and hwclock. Whether you are a sysadmin debugging a production issue or a developer writing shell scripts, these tools will save you time.

The date Command: Your Primary Tool

The date command is the Swiss Army knife of time manipulation on Linux. It can display the current time, convert timestamps, format output, and even set the system clock.

Getting the Current Unix Timestamp

# Current Unix timestamp (seconds since epoch)
date +%s
# Output: 1753401600

# Current time in milliseconds (for JavaScript compatibility)
echo $(($(date +%s%N) / 1000000))
# Output: 1753401600000

# Current time in nanoseconds
date +%s%N
# Output: 1753401600123456789

The %s format specifier is the key. It tells date to output the number of seconds since the Unix epoch.

Converting a Timestamp to a Readable Date

# Convert Unix timestamp to human-readable date
date -d @1753401600
# Output: Sat Jul 25 00:00:00 UTC 2026

# Custom format
date -d @1753401600 +"%Y-%m-%d %H:%M:%S"
# Output: 2026-07-25 00:00:00

# ISO 8601 format
date -d @1753401600 --iso-8601=seconds
# Output: 2026-07-25T00:00:00+00:00

The @ prefix tells date that the following number is a Unix timestamp. Without it, date tries to parse the number as a date string.

Converting a Date String to a Timestamp

# Convert a date string to Unix timestamp
date -d "2026-07-25 00:00:00 UTC" +%s
# Output: 1753401600

# Relative dates
date -d "tomorrow" +%s
date -d "next monday" +%s
date -d "2 weeks ago" +%s

# Specific time zones
date -d "2026-07-25 00:00:00 EST" +%s
date -d "2026-07-25 00:00:00 Asia/Shanghai" +%s

The -d flag accepts a wide range of date formats, including natural language like "tomorrow" and "next monday". This is a GNU extension and may not work on macOS or BSD systems.

Formatting Output

The date command supports dozens of format specifiers. Here are the most useful ones:

# Common formats
date +"%Y-%m-%d"           # 2026-07-25
date +"%H:%M:%S"           # 12:30:45
date +"%Y-%m-%d %H:%M:%S"  # 2026-07-25 12:30:45

# Week and day info
date +"%A %B %d, %Y"       # Saturday July 25, 2026
date +"Week %V of %Y"      # Week 30 of 2026

# Unix timestamp
date +%s                   # 1753401600

# Timezone info
date +"%Z %z"              # UTC +0000

For a complete list of format specifiers, run man date or date --help.

timedatectl: Managing System Time and Timezones

Modern Linux distributions use systemd, which includes the timedatectl command for managing system time, timezones, and NTP synchronization.

Checking Current Time Settings

# Show current time configuration
timedatectl status

# Example output:
#               Local time: Sat 2026-07-25 12:30:45 UTC
#           Universal time: Sat 2026-07-25 12:30:45 UTC
#                 RTC time: Sat 2026-07-25 12:30:45
#                Time zone: UTC (UTC, +0000)
# System clock synchronized: yes
#              NTP service: active
#          RTC in local TZ: no

This command shows you everything: local time, UTC time, hardware clock time, timezone, and whether NTP is synchronized.

Listing Available Timezones

# List all available timezones
timedatectl list-timezones

# Filter by region
timedatectl list-timezones | grep -i asia
timedatectl list-timezones | grep -i america

# Common timezones
# Asia/Shanghai
# America/New_York
# Europe/London
# UTC

Changing the System Timezone

# Set timezone to UTC
sudo timedatectl set-timezone UTC

# Set timezone to a specific region
sudo timedatectl set-timezone Asia/Shanghai
sudo timedatectl set-timezone America/New_York

# Verify the change
timedatectl status

Changing the timezone affects how date displays local time, but Unix timestamps remain unchanged (they are always UTC).

Enabling NTP Synchronization

# Enable automatic time synchronization
sudo timedatectl set-ntp true

# Disable NTP (not recommended for production)
sudo timedatectl set-ntp false

# Check NTP status
timedatectl status | grep "NTP service"

NTP (Network Time Protocol) keeps your system clock synchronized with internet time servers. This is critical for servers that generate logs, handle authentication tokens, or coordinate with other systems.

hwclock: Hardware Clock Management

The hardware clock (also called RTC or CMOS clock) is a battery-powered clock on your motherboard that keeps time even when the system is powered off. Linux maintains two clocks: the hardware clock and the system clock (software clock).

Reading the Hardware Clock

# Show hardware clock time
hwclock --show

# Show hardware clock in UTC
hwclock --show --utc

# Show hardware clock in local time
hwclock --show --localtime

Synchronizing Clocks

# Set system clock from hardware clock
sudo hwclock --hctosys

# Set hardware clock from system clock
sudo hwclock --systohc

# Set hardware clock to UTC (recommended)
sudo hwclock --systohc --utc

The --hctosys flag copies the hardware clock time to the system clock. The --systohc flag does the reverse. In most cases, you want the hardware clock to be in UTC and let the system handle timezone conversion.

Why Hardware Clock Matters for Timestamps

If your hardware clock drifts or is set to the wrong timezone, timestamps in your logs and databases will be incorrect. This is especially problematic for:

  • Log analysis: Timestamps in /var/log/syslog or application logs will be wrong
  • Certificate validation: SSL/TLS certificates have validity periods based on system time
  • Cron jobs: Scheduled tasks may run at unexpected times
  • Database records: created_at and updated_at fields will have incorrect values

Practical Shell Script Examples

Converting Timestamps in a Log File

#!/bin/bash
# Convert Unix timestamps in a log file to readable dates

while IFS= read -r line; do
  # Extract timestamp (assumes first field is a Unix timestamp)
  timestamp=$(echo "$line" | awk '{print $1}')

  # Convert to readable date
  readable=$(date -d "@$timestamp" +"%Y-%m-%d %H:%M:%S")

  # Replace timestamp in line
  echo "$line" | sed "s/$timestamp/$readable/"
done < access.log

Calculating Time Differences

#!/bin/bash
# Calculate the difference between two timestamps

start_ts=1753401600
end_ts=1753488000

diff=$((end_ts - start_ts))
hours=$((diff / 3600))
minutes=$(((diff % 3600) / 60))

echo "Difference: $hours hours and $minutes minutes"
# Output: Difference: 24 hours and 0 minutes

# Convert to human-readable format
echo "Start: $(date -d @$start_ts)"
echo "End: $(date -d @$end_ts)"

Generating Timestamps for Testing

#!/bin/bash
# Generate timestamps for the last 7 days

for i in {0..6}; do
  ts=$(date -d "$i days ago" +%s)
  readable=$(date -d "@$ts" +"%Y-%m-%d")
  echo "$ts -> $readable"
done

# Output:
# 1753401600 -> 2026-07-25
# 1753315200 -> 2026-07-24
# 1753228800 -> 2026-07-23
# ...

Monitoring File Modification Times

#!/bin/bash
# Check when a file was last modified (as Unix timestamp)

file="/var/log/syslog"

# Get modification time as Unix timestamp
mod_ts=$(stat -c %Y "$file")
mod_date=$(date -d "@$mod_ts" +"%Y-%m-%d %H:%M:%S")

echo "File: $file"
echo "Modified: $mod_date (timestamp: $mod_ts)"

# Check if file was modified in the last hour
current_ts=$(date +%s)
age=$((current_ts - mod_ts))

if [ $age -lt 3600 ]; then
  echo "File was modified in the last hour"
else
  echo "File was modified $((age / 3600)) hours ago"
fi

Linux vs macOS: Key Differences

If you work on both Linux and macOS, be aware that the date command behaves differently:

Task Linux (GNU date) macOS (BSD date)
Current timestamp date +%s date +%s
Timestamp to date date -d @1753401600 date -r 1753401600
Date to timestamp date -d "2026-07-25" +%s date -j -f "%Y-%m-%d" "2026-07-25" +%s
Relative dates date -d "tomorrow" Not supported

The -d flag is a GNU extension. On macOS, use -r to interpret a number as a timestamp, and -j -f to parse a date string.

Using Online Tools as a Complement

While command-line tools are powerful, sometimes you just need a quick conversion without typing a command. Online tools are handy for:

  • Quick lookups: Paste a timestamp and see the date instantly
  • Visual comparison: See multiple timestamp formats side by side
  • Sharing results: Send a link to a colleague with the conversion

The FastUnix Timestamp Converter works entirely in your browser and supports both seconds and milliseconds formats. It is especially useful when you are on a machine without terminal access or need to share a timestamp conversion with a non-technical colleague.

FAQ

Why does date -d @timestamp not work on my system?

The -d flag is a GNU extension available on Linux. If you are on macOS or BSD, use date -r timestamp instead. For portable scripts, check the OS first:

if [[ "$OSTYPE" == "darwin"* ]]; then
  date -r "$timestamp"
else
  date -d "@$timestamp"
fi

How do I convert a timestamp in a specific timezone?

Unix timestamps are always UTC. To see what time a timestamp represents in a specific timezone, set the TZ environment variable:

TZ='Asia/Shanghai' date -d @1753401600
# Output: Sat Jul 25 08:00:00 CST 2026

TZ='America/New_York' date -d @1753401600
# Output: Fri Jul 24 20:00:00 EDT 2026

What is the difference between date and timedatectl?

The date command displays and formats time. The timedatectl command manages system time settings (timezone, NTP, hardware clock). Use date for conversions and formatting; use timedatectl for configuration.

Why are my log timestamps wrong after daylight saving time?

If your system timezone observes DST, the offset changes twice a year. Unix timestamps are unaffected (they are always UTC), but the local time display shifts. To avoid confusion, store and display timestamps in UTC, or use timedatectl to set a timezone that does not observe DST (like UTC itself).

How do I get millisecond precision on Linux?

The date command supports nanosecond precision with %N:

# Milliseconds
echo $(($(date +%s%N) / 1000000))

# Microseconds
echo $(($(date +%s%N) / 1000))

# Nanoseconds
date +%s%N

Note that the actual precision depends on your system clock. Most Linux kernels provide microsecond precision; nanosecond precision is available on newer kernels with high-resolution timers.

Conclusion

Linux provides a comprehensive set of command-line tools for working with Unix timestamps. The date command handles most conversion tasks, timedatectl manages system time configuration, and hwclock ensures your hardware clock stays accurate.

Key takeaways:

  • Use date +%s to get the current Unix timestamp
  • Use date -d @timestamp to convert a timestamp to a readable date (Linux) or date -r timestamp (macOS)
  • Keep your hardware clock in UTC and let the system handle timezone conversion
  • Enable NTP synchronization to prevent clock drift
  • Use online tools like the FastUnix Timestamp Converter for quick lookups when you do not have terminal access

For related tasks, the FastUnix JSON Formatter helps you inspect timestamp values in API responses, and the URL Encoder handles timestamp encoding in URL parameters and shell scripts.