Date Command in Linux/Unix with Practical Examples

The date command in Linux/Unix, although seemingly simple, has many applications – creating unique names to use with logfiles and screenshots, energy-efficient alarm clock, finding times in different time zones, finding date 1 year before today, converting Unix Timestamps to DateTime format or any other formats, finding the last modified time of a file, and many more. In this article, I will talk about all features of the date command using such applications.

Table of Contents

1. How to Get Current Date and Time Using Date Command in Linux

One problem I very often face while booting back and forth between Windows and Linux in my dual-boot PC is that the current time gets changed. While fixing this up, I use the date command very often.

Syntax:

~$ date

Output:

Tue Dec 14 02:54:05 AM IST 2021

Combining the above command with the watch command you can even create a digital clock that will print the date and time at every second 😁:

~$ watch --interval=1 date

You can also get the Coordinated Universal Time (UTC) using -u or –utc or –universal flag:

~$ date --utc

Output:

Mon Dec 13 09:25:50 PM UTC 2021

In the above examples, as you are seeing, the outputs are in a special order – first weekday (Mon), then the year (Dec), then the day of the month (13), and so on. We can change this order of output and that will be described in the next heading.

2. How to Get Current Time and Date in Given Formats Using Date Command in Linux

To change the output format, we use special sequences. The following sequences are sufficient to fulfill most of your needs:

Format SequencesInterpretation
%aabbreviated weekday name (e.g., Sun)
%Afull weekday name (e.g., Sunday)
%babbreviated month name (e.g., Jan)
%Bfull month name (e.g., January)
%dday of the month (e.g., 01)
%Hhour (e.g., 00..23)
%Ihour (e.g., 01..12)
%mmonth (01..12)
%Mminute (00..59)
%Yyear
%Nnanoseconds (000000000..999999999)
%pAM or PM
%Pam or pm
%sseconds since the Epoch (1970-01-01 00:00 UTC)
%Ssecond (00..60)
%uday of the week (1..7); 1 is Monday
Table: Date Sequences and Their Interpretation; Source: man date

There are more such sequences. To learn about them, see the man page.

Syntax:

~$ date +FORMAT

2.1. Example 1. Creating Unique Filename

~$ date '+%Y-%m-%d_%T.%N'

Output:

2021-12-20_23:11:42.983703647

My screenshot command is based on the above example:

~$ import "$(date '+%Y-%m-%d_%T.%N').png"

Here import is a screenshot tool included in the package imagemagick and "$(date '+%Y-%m-%d_%T.%N').png" is the name of the image where the screenshot will be saved.

The beauty of the above date based command:

  1. Times in nano-seconds (%N) make sure that two files created at the interval of even a nano second have different names so that they don’t overwrite each other.
  2. Sorting based on times becomes easy.

To verify the beauty, you can just execute the given screenshot command couple of times in your terminal. Here is how the output files will look like:

2021-12-20_23:13:10.639718047.png
2021-12-20_23:13:13.580715441.png
2021-12-20_23:13:18.507580139.png
2021-12-20_23:13:20.517476383.png
2021-12-20_23:13:23.277434727.png
2021-12-20_23:13:36.722378523.png
2021-12-20_23:13:38.646510476.png
2021-12-20_23:13:41.255368267.png

Similarly, Linux system administrators can also create log files with unique names.

2.2. Example 2: Creating Clock for Tiling Window Managers

Another example is what I use as my clock in my i3-tiling window manager:

~$ date '+%d/%m %I:%M %u'

Output:

14/12 03:27 2

Notice how in the above example special characters like colon (:), slash (/), and spaces are printed without any changes.

Clock in i3bar using date command
Figure: clock in i3-tiling window manager using the date command

All of the above date commands were related to ‘current’ time. What if we want to manipulate times other than the current one?

3. How to Print a Given Time Instead of the Current Time Using Date Command in Linux

This is done using the -d or –date flag. Here, the date command’s --date flag is somewhat similar to the --date flag in the touch command.

Syntax:

~$ date --date=STRING
~$ date -d STRING

Here, date parses the STRING in human-readable form.

-d STRINGMeaning
-d '14:02'14:02 @ Today
-d 'yesterday'current time @ yesterday
-d 'today'current day
-d 'next sunday'upcoming Sunday
-d 'tomorrowcurrent time but tomorrow
-d 'June 15, 2004'June 15 2004 @ 12:00:00 AM
-d '@1619076682Thu Apr 22 01:01:22 PM IST 2021; explained below
-d '00:03:00'00:03:00 @ Today
-d '-1 hour'1 hour ago
-d '+1 hour'1 hour later

There are many such STRING formats given at GNU documentation of date.

Example:

~$ date; date -d '1 year'

The above example contains two commands separated by a semicolon.

Output:

Tue Dec 14 05:58:02 AM IST 2021
Wed Dec 14 05:58:02 AM IST 2022

In the output, the first line is for the first date command and the second line is for the second date command. Notice the difference of 1 year:

The -d flag can be used with +FORMAT to convert dates from one format into another. This is highly useful for Linux System Administrators where they very often need to analyze the log files.

4. How to Convert Dates from One Format to Another Using Date Command in Linux

Syntax:

~$ date -d STRING +FORMAT

One very good example will be the conversion of dates given in the /var/log/pacman.log file in Archlinux into a more readable format:

~$ date -d '2021-12-13T02:07:20+0530' '+%d/%m/%Y %I:%M %p %A'     

Output:

13/12/2021 02:07 AM Monday

Another very important conversion is to/from Unix Timestamp.

5. Convert Unix Timestamp to Date and Vice-Versa Using Date Command in Linux

Before you begin, first you will need to know what is Unix Timestamp.

5.1. Unix Timestamp

It is a time elapsed since 00:00:00 UTC on 1 Jan 1970. 00:00:00 UTC on 1 Jan 1970 is called Unix Epoch.

In order to find the current Unix Timestamp, you need to use the following command:

~$ date '+%s'

Output:

1639444603

5.2. Date to Unix Timestamp

This is very simple. You just need to use +%s output format.

Example:

~$ date -d '2021-04-22 04:39:00' '+%s'

Output:

1639444957

Another fine example will be its usage with rtcwake command which wakes up the computer from the suspend mode at the given time:

~$ sudo rtcwake --mode mem --time "$(date -d "30 minutes" '+%s')"

The above command suspends my computer immediately and it will wake it up 1 hour later.

😀 An energy-efficient alarm tool: I combine the above command with the ffplay command (included in the FFmpeg package) just before I take a short nap. It even suspends my PC and thus saving electricity. 30 minutes later my PC wakes up and then the alarm goes up. The overall command is:

~$ sudo rtcwake --mode mem --time "$(date -d "30 minutes" '+%s')" ; ffplay ~/Music/alarm.mp3

5.3. Unix Timestamp to Date

To convert Unix Timestamp to any other format, you need to prepend @ with it in the -d flag.

Syntax:

~$ date -d @UNIXTIMESTAMP +FORMAT

Example:

~$ date -d '@1639444957' '+%d/%m/%Y %I:%M %p %A'
14/12/2021 06:52 AM Tuesday

6. How to Get Last Modified Time of a File Using Date Command in Linux

For this, we use the –reference or -r flag.

Syntax:

~$ date --reference=FILENAME
~$ date -r FILENAME

Example:

~$ date --reference="/etc/lightdm/lightdm.conf"

Output:

Wed Jul  8 12:28:15 AM IST 2020

😃 Fun Fact: Linux treats directories, optical discs, CPUs, batteries, and everything else as files. So, you can also use a directory in place of FILENAME.

~$ date --reference="/etc"        
Tue Dec 14 01:27:33 AM IST 2021

7. How to Get Times in Different Time Zone Using Date Command in Linux

All the above date commands can be prepended with TZ=TIME_ZONE. For example, to find the time in Paris, we can use the following command:

~$ TZ='Europe/Paris' date

Output:

Tue Dec 14 09:06:36 PM CET 2021

You might be wondering how can you find the TZ (Europe/Paris in the above example). For that, use the tzselect command as I did in my case:

Using tzselect to find time zone in Linux
Figure: Using tzselect to find time zone in Linux

8. Conclusion

There are many such practical examples. The key is constantly using the date command. To learn more, read info date, man date, and GNU guide on date documents.

Thank you guys for staying up to this point. I have tried my best to cover everything without committing any mistakes. If you have any further questions, or there are some mistakes in the article, please notify me through the comment section below.

Leave a Comment

Your email address will not be published. Required fields are marked *