The date command on a Linux system is a very versatile command that can be used for many functions. Among them is the ability to calculate a file's creation date, last modified time, etc. This can be built into a script, used for scheduling, or just used to obtain basic information about a file or directory on the system.
The date command can also handle addition and subtraction arithmetic to help calculate dates and times. It uses Unix's epoch time as a base of reference, which is 00:00:00 UTC on January 1, 1970. From this date, it can assign a timestamp based on the number of seconds something occurred before or after it.
These timestamps are great for calculation, but hardly usable for people. It'd take a machine to know that
Thu 02 Jun 2016 12:59:59 PM UTC translates to
1464872399. In this guide, we'll explain how to convert a timestamp to a human readable date and vice versa.
- How to convert a timestamp to date
|Category||Requirements, Conventions or Software Version Used|
|System||Any Linux distro|
|Other||Privileged access to your Linux system as root or via the |
|Conventions|| # - requires given linux commands to be executed with root privileges either directly as a root user or by use of |
Convert timestamp to date in bash
To convert a timestamp to a human-readable date, use the following command syntax and substitute your own timestamp in place of the one in this example. Note that this will return the time in UTC.
$ date -ud @1464872499 Thu 02 Jun 2016 01:01:39 PM UTC
If you'd like to get the time for your system's currently configured time zone, you can omit the
$ date -d @1464872499 Thu 02 Jun 2016 09:01:39 AM EDT
Convert date to timestamp in bash
To convert a date to epoch timestamp, use the following syntax.
$ date -d '06/02/2016 09:01:39' +"%s" 1464872499
Note that the
-d option is expecting you to specify the date in American format (MM/DD/YYY). The command also assumes we're using our current time zone since the
-u option hasn't been specified.
You could also use this format. Feel free to omit the day of the week (Thursday in this case), or not. Notice the
-u option is unnecessary here since UTC is explicitly mentioned.
$ date -d 'Thu 02 Jun 2016 01:01:39 PM UTC' +"%s" 1464872499
Get current date and timestamp
You may need to grab the current date or timestamp to compare modification times or perform other functions (i.e. when writing bash scripts). Check the commands below for instructions on how to do that.
Date in current time zone:
$ date Mon 07 Sep 2020 12:21:37 AM EDT
Date in UTC:
$ date -u Mon 07 Sep 2020 04:21:47 AM UTC
$ date +"%s" 1599452525
In this guide, we've shown how to use the
date command in bash to convert Unix epoch timestamps to human readable dates, and vice versa. The date command is very wieldy and intuitive once you read up on its options and understand how timestamps work, which we've done in this tutorial.