date Command in Linux | Explained

In Linux, the date command is one of them and is used to display and set the date/time this command line utility comes by default in most of the Linux distributions as it offers to manage the specific functionality of the system. This post will illustrate the working and usage of the date command in Linux, and also, and the outcomes are as follows:

Let’s start!

What is the Date Command Utility in Linux?

The date command displays the date in different formats and configures the date using the terminal. The syntax of using the date command in Linux is shown below:

Syntax:

$ date [Option] [Format]

The options that can be used with the date command are:

OptionsExplanation
-dIt is used to display the date in a string.
-RIt is used to display the date in RFC format.
-rIt is used to display the last time of file modification.
-sIt is used to set the time by the assigned string of dates.
-uIt is used to display the time in the UTC format.
–helpIt displays more information about the date command utility.
–versionIt displays the installed version of the date command utility.

Also, different formats options can be used to display the date in Linux:

FormatsExplanations
%aIt displays the abbreviation of the weekdays.
%AIt displays the full form of the weekday.
%bIt displays the abbreviation of the month.
%BIt displays the full form of the month.
%cIt displays the local’s date.
%CIt displays the century date.
%yIt displays the last two digits of the year.
%dIt displays the day of the month.
%DIt displays the date in month/date/year format.
%FIt displays the full date.
%YIt displays the year.
%gIt displays the last two digits of the iso week.
%GIt displays the year of the iso week.
%HIt displays the hour in a 24-hours format.
%hIt displays the hour in a 12-hours format.
%jIt displays the day of the year.
%mIt displays the minutes.
%MIt displays the Months.

The date command in Linux is used with different options and formats. For more details on options and formats, use the below-stated command:

$ date --help

Here, you will get all the options related to the date command.

What is the Usage of the date Command Utility in Linux?

As the name of the command represents, the purpose of the date command is associated with the date/time of your Linux system. Let’s practice this command:

Example 1: Display the Current Date and Time

To display the date using the terminal, run the date command:

$ date

Display the Date as a String

If you want to display the string of the date, then use the “-d” option:

$ date -d “2022-10-05 12:10:53”

Example 2: Display the Upcoming Dates

The date command can be used to get the future date by defining the period in days, weeks, months, etc. Let’s experience a few of these kinds of date command usage:

Date after 30 Days

To display the date after 30 days, use the below mentioned-command:

$ date --date="30 days"

Date on Tomorrow

We can also display the date by passing the strings of “yesterday” and “tomorrow‘:

$ date -d “tomorrow”

Example 3: Display the Date in Specific Format

Similarly, we can display the date in different formats by using the “%y, %m, and %d”. For example, to display the date in a year:month: day format, use the command:

$ date +" %Y:%m:%d"

Display the Date in ‘Days:Month:Year’ Format

To display the date in the format mentioned above, use the command:

$ date +" %d:%m:%Y"

Similarly, the other formatting options can also be utilized.

How to Set the Date Using the date Command in Linux?

To set the date using the terminal, we can use the date command; for example, we will set the date using the command:

$ sudo date --set="20221001 05:30"

In the above command, we have set the date to 2022-10-01 with the time 05:30.

That’s all about the date command tutorial.

Conclusion

The date command utility in Linux is used to display the date and set the date using the terminal. This command utility supports different options and formats to get the date as per the requirement of the user. In this blog, the usage and working of the date command utility have been demonstrated with the help of some examples in detail.