Table of Contents
On Linux systems with GNU date, use date +%s to print the current Unix timestamp and date -u -d @TIMESTAMP to convert epoch seconds into a UTC date. These commands read and format time; they do not change the system clock.
Unix epoch time counts seconds from 1970-01-01 00:00:00 UTC. It is useful for comparing log events and exchanging timestamps through APIs. Typical Unix/POSIX timestamps do not count leap seconds.
Print the current timestamp
date +%sThe result is an integer number of seconds. It changes every second, so your output will differ from a fixed example. To print a readable current UTC time instead, use:
date -u '+%Y-%m-%d %H:%M:%S %Z'Convert epoch seconds to a UTC date
Prefix a seconds value with @ and pass it to -d:
date -u -d @1618921342 '+%Y-%m-%d %H:%M:%S %Z'Output:
2021-04-20 12:22:22 UTCHere, -u selects UTC, -d parses the supplied time instead of reading the current clock, and the format string controls the output. Without -u, the same timestamp is displayed using the system's default time zone.
This example is April 20 at 12:22:22 UTC, not 10:12:22 UTC. When a tutorial's date and timestamp disagree, verify the conversion instead of carrying the mismatch into a script.
Convert a readable date to epoch seconds
Specify the time zone explicitly so the result does not depend on the computer's local setting:
date -d '2021-04-20 12:22:22 UTC' +%sOutput:
1618921342Use an unambiguous year-month-day date and quote the entire input string. For reference, 2021-04-20 10:12:22 UTC converts to 1618913542, a different timestamp. GNU's date examples explain both conversion directions.
Display the same moment in another time zone
Set TZ for this one command:
TZ='America/New_York' date -d @1618921342 '+%Y-%m-%d %H:%M:%S %Z'Output with the applicable time-zone rules:
2021-04-20 08:22:22 EDTThe epoch value has not changed; only its calendar representation has. A named zone such as America/New_York applies the rules for that location and date, including daylight saving time. A fixed UTC offset does not automatically follow seasonal changes.
This command does not change the computer's configured zone. For a permanent configuration change, see TipsMake's Linux time-zone guide. Keep the operating system's time-zone database updated when accurate regional rules matter.
Check whether the input uses seconds or milliseconds
APIs do not all use the same unit. A value such as 1618921342000 is the millisecond equivalent of the seconds example above. Confirm the API's documentation before converting; a long number alone does not prove its unit.
For this integer millisecond value, divide by 1000 to convert to whole seconds:
timestamp_ms=1618921342000
date -u -d "@$((timestamp_ms / 1000))" '+%Y-%m-%d %H:%M:%S %Z'This example intentionally discards sub-second precision. If fractional seconds matter, use a conversion method that preserves them rather than integer division. Validate external input before inserting it into shell arithmetic.
Avoid changing the clock for a conversion
The -s or --set option changes system time and generally needs administrative privileges. It is unnecessary for converting a log timestamp and can affect running services, certificates, and scheduled jobs. Use -d for parsing and formatting.
These examples use GNU date, common on Ubuntu and other GNU/Linux distributions. BSD/macOS date and some minimal implementations have different options. Consult your local man date if -d is not recognized. GNU's epoch reference documents the underlying count, and TipsMake's Python time module guide covers related application-level timestamps.
Reader Comments 0
Sign in with email or Google to join the discussion.