Convert Epoch Timestamp to Readable Date in Linux
In the Linux operating system, Unix epoch timestamps—which count the
number of seconds elapsed since January 1, 1970 (UTC)—can be converted
into human-readable date and time formats using the native
date command. By passing the @ symbol followed
by the timestamp to the -d or --date flag,
Linux parses the numeric value and translates it into a standard
calendar date, allowing users to customize timezones and output formats
directly from the command line.
The Basic Conversion Syntax
The GNU date utility found in most Linux distributions
uses the -d (or --date) option to accept input
strings. To specify an epoch timestamp, prefix the numeric value with
the @ symbol:
date -d @1704067200Output:
Mon Jan 1 00:00:00 UTC 2024
(Note: The actual output time zone will default to your system's configured local timezone.)
Converting to UTC
By default, the date command displays the converted
epoch time according to the local timezone configured on your system. To
view the timestamp in Coordinated Universal Time (UTC), add the
-u (or --utc) flag:
date -u -d @1704067200Customizing the Output Format
The date command allows formatting the output using
format specifiers prefixed by a + sign. This allows you to
extract specific date components or adhere to standard formats like ISO
8601.
Standard Date and Time (YYYY-MM-DD HH:MM:SS):
date -d @1704067200 +"%Y-%m-%d %H:%M:%S"Output:
2024-01-01 00:00:00Date Only:
date -d @1704067200 +"%F"Output:
2024-01-01Time with Timezone:
date -d @1704067200 +"%T %Z"Output:
00:00:00 UTC
Handling Milliseconds
The standard date command expects epoch values in
seconds. If you have a 13-digit millisecond timestamp (common in web
applications), you must convert it to seconds before passing it to
date. This can be done in Bash using arithmetic
expansion:
epoch_ms=1704067200000
date -d @$((epoch_ms / 1000))BSD/macOS Differences
While GNU date is standard across Linux, macOS and BSD
variants use a different syntax for epoch conversion. On BSD systems,
the -r flag is used instead:
date -r 1704067200