Linux Commands

Linux Date Command

The date, time, local time, etc. are displayed using the Linux date command. To modify the time and date of the Linux system, we can also use the date command. This command has many options, including “-d,” “-u,” and “-r,” to carry out diverse operations. Since “-d” and “-date” are identical, we can use “-date” or “-d” to display the date and time of the past and future. In this article, we will also employ a few format specifiers with the date command to obtain the date and time in various formats.

Using the Linux Date Command

The date function is utilized to display or update the value of the system’s dates and times on Unix-like hardware and software. In Linux, we can quickly obtain the time and date by using the command “date” in the terminal window. This will show the time and date as they are at the moment. Although the date command has a variety of flags. In this section we will just use the term “date” with no other options to display the system’s date and time. When we enter the command “date” in the terminal window, the current date is displayed on the screen.

omar@omar-VirtualBox:~$ date

As shown, the output below displayed the current date and time. It will show the abbreviated name of the day, “Tue,” which stands for “Tuesday” the abbreviated name of the month “Jan,” which stands for “January,” the day of the month, and the current time, “01:54:36,” which is presented in the format “hour/minutes/second” and additionally showed the year which is “2023”.

Tue Jan 11 01:54:36 AM PKT 2023

Date String

The date that we supplied in the command as a string will be displayed in date format in the output in this part as we will simply display the given date string in date format. We have two possibilities when using the Linux date command to complete this “-date” or “-d.” These two will convert the provided date string into the appropriate date format. Let us begin the command by typing “date” at the beginning, followed by the flag “-date,” the equal sign “=”, and finally the date string format “10/01/2023”.

omar@omar-VirtualBox:~$ date - -date=”10/01/2023

When we run this command, the string format is changed to a date format as you can see from the date that is presented along with the day, time, and year.

Tue Jan 10 12:00:00 AM PKT 2023

Using the -date Option to Display Past Dates and Times

In this section, we will show you how to use the “-date” parameter in the date command to display dates and times from the past. You may also use the “-d” flag for the same purpose.

Yesterday’s Time and Date

Using the “-date” option of the date command, this section will display the time and date from yesterday. To do this, we use a command in which the term “date” is used first, followed by the option “date” with a double-dash “- -,” equals signs “=”, and finally the word “yesterday” inside of an inverted comma. The word “yesterday” is used because we want to display the date from yesterday.

omar@omar-VirtualBox:~$ date - -date=”yesterday”

The date from yesterday is shown when we run the command, as can be seen. This includes the day, year, and date.

Mon Jan 9 02:11:44 AM PKT 2023

The Time and Date Two Years aAo

The time and date from the previous two years are displayed in this section. To display the time and date from two years ago, we will use the same command as in the previous command but here we specified the string “2 year ago” inside of the inverted comma.

omar@omar-VirtualBox:~$ date - -date=”2 year ago”

The output, which included the time and date from two years previous, was shown. The fact that the date and time are the same, indicating that it is currently today. But that the year has been changed to one that is two years ago “2021” means that the command has been successful. Additionally, the day that was “Tuesday” on that day, two years ago is displayed.

Tue Jan 10 02:09:07 AM PKT 2021

The Time and Date Three Seconds Ago

The date and time from three seconds ago will be shown in this part. To do this, we will use the same command and flag, “-date” or “-d,” but this time we will supply the text “3 seconds ago” to the command to obtain the date and time from three seconds ago.

omar@omar-VirtualBox:~$ date - -date=”3 sec ago”

Following the execution of this command, the time and date from three seconds ago were shown. The time is modified since it was the time that was before three seconds, therefore the time before three seconds was “02:10:35,”. The minutes and hours are the same. but the second is different. The day, month, and date are the same “Tuesday”, “January” and “10” respectively.

Tue Jan 10 02:10:35 AM PKT 2023

Similarly, by using this command with the same option, we may locate the dates and times of the past. For instance, if we want to display the date from three months ago, we will enter three months ago in the command within the inverted comma. The same option in Linux allows us to find future dates as well so the output will show the future time and date in the next part.

Date and Time of Next Sunday

The time and date of next Sunday will be located and displayed in this section. The date will change because it will display the date that will occur next Sunday in the future. To display the date of the next Sunday, we are going to use the command in which we first type the keyword “date,” followed by the flag “date,” with a double dash, the equals sign “=”, and finally the text “next sun”.

omar@omar-VirtualBox:~$ date - -date=”next sun”

When the command was executed, the time and date of the coming Sunday were displayed. It presented the format in which it first displayed the day, month, and date so that the upcoming Sunday’s date will be “15,” which is accurate. Then, it displayed a random time along with the year, which is accurately displayed.

Sun Jan 15 12:00:00 AM PKT 2023

Formatting Options

In Linux, the date command can be used with a variety of format specifiers to obtain the date, time, and other information. These characters are “%D,” “%T,” “%m,” “%d,” etc.

Using %D with Date Command

The format of the date, which comprises the month, date, and year, is displayed simply using the option “%D”. For this, we use a command in which the keyword “date” is initially entered, followed by the symbol “%D” with the addition symbol “+” inside the inverted comma.

omar@omar-VirtualBox:~$ date “+%D”

After running this command, the date “01/10/23” is displayed. The format of this date is “month/date/year”.

01/10/23

Using the Date Command with %D and %T

The two flags “%D” and “%T” will be used in this section. “%D” only displays the date, but we will also use “%T” to display the time since “%T” is used to display the time in the format hour/minute/second. To accomplish this, we will use the same command as above but this time, in addition to the option “%D,” we’ll also use the “%T” flag.

omar@omar-VirtualBox:~$ date%+D %T”

As a result, after it is executed, we can see that it outputs both the date and the time.

01/10/23 02:34:05

Conclusion

The Linux date command, which is used to show the system’s date and time on the terminal, is covered in this article. In the above scenario, we used various examples and displayed multiple dates and times. The aforementioned uses the date command’s “-date” option to display previous and future dates on the terminal. To obtain the date and time in a different format, we have also utilized some format specifiers with the date command, such as “%D” and “%T.”

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.