How to Convert Timestamp to Date in Bash (With Example)


Often you may want to convert a Unix timestamp to a date using Bash.

You can use the following methods to do so:

Method 1: Convert Timestamp to Date

date -d @1667614523

This particular example will convert the timestamp 1667614523 into a human-readable date.

Method 2: Convert Timestamp to Date (With Specific Format)

date -d @1667614523 +"%Y-%m-%d %H:%M:%S"

This particular example will convert the timestamp 1667614523 into a human-readable date, displayed using a specific format.

Note: We use the -d operator to specify that we would like to display the date described by the string that follows it. Without this operator, the date command would simply return the current date and time.

The following examples show how to use each method in practice.

Example 1: Convert Timestamp to Date in Bash

Suppose that we would like to convert the Unix timestamp 1667614523 into a human-readable date.

We can use the following syntax to do so:

date -d @1667614523 

The following screenshot shows how to use this syntax in practice:

Bash convert timestamp to date

This returns the following date:

  • Fri Nov 4 22:15:23 EDT 2022

This represents the Unix timestamp that we provided as a human-readable date.

Example 2: Convert Timestamp to Date (with Specific Format) in Bash

Suppose that we would like to convert the Unix timestamp 1667614523 into a human-readable date with a specific format.

We can use the following syntax to do so:

date -d @1667614523 +"%Y-%m-%d %H:%M:%S"

The following screenshot shows how to use this syntax in practice:

Bash convert timestamp to date with specific format

This returns the following date:

  • 2022-11-04 22:15:23

Note that we used the + operator to specify a date format to use.

Feel free to specify any format for the date that you would like. For example, you could use the following syntax to only display the date without the time:

date -d @1667614523 +"%Y-%m-%d"

The following screenshot shows how to use this syntax in practice:

Bash convert timestamp to date without time

This returns the following result:

  • 2022-11-04

Feel free to use similar syntax to specify any date format that you would like.

Related Tutorials

The following tutorials explain how to perform other common tasks in Bash:

How to Compare Dates in Bash
How to Calculate Date Difference in Bash
How to List All Files in Folder by Date in Bash

Leave a Reply