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:
- What is the date Command Utility in Linux?
- What is the Usage of the date Command Utility in Linux?
- How to Set the Date/Time in Linux Using the date Command?
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:
Options | Explanation |
---|---|
-d | It is used to display the date in a string. |
-R | It is used to display the date in RFC format. |
-r | It is used to display the last time of file modification. |
-s | It is used to set the time by the assigned string of dates. |
-u | It is used to display the time in the UTC format. |
–help | It displays more information about the date command utility. |
–version | It displays the installed version of the date command utility. |
Also, different formats options can be used to display the date in Linux:
Formats | Explanations |
---|---|
%a | It displays the abbreviation of the weekdays. |
%A | It displays the full form of the weekday. |
%b | It displays the abbreviation of the month. |
%B | It displays the full form of the month. |
%c | It displays the local’s date. |
%C | It displays the century date. |
%y | It displays the last two digits of the year. |
%d | It displays the day of the month. |
%D | It displays the date in month/date/year format. |
%F | It displays the full date. |
%Y | It displays the year. |
%g | It displays the last two digits of the iso week. |
%G | It displays the year of the iso week. |
%H | It displays the hour in a 24-hours format. |
%h | It displays the hour in a 12-hours format. |
%j | It displays the day of the year. |
%m | It displays the minutes. |
%M | It 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.