The date command depicts the date and time of the system. It permits the user to get both date and time in different formats. This command also helps in calculate current, past, and future dates with the help of different operators and format specifiers. For functionalities to be performed, the system clock and clock of the server should be precisely on time. Here are some examples that will increase your knowledge about the date command in Linux.
To learn the functionality of date commands and their usage, Linux(Ubuntu) should be installed on your system. After installation, you have to configure Linux by providing user details, including name and password, to access the applications. The image below shows the interface of Ubuntu. Here you need to provide a password to proceed further.
A basic command used to display date is to type “Date”. It brings the current date and time which is automatically received from the system. The output shows the day of the month, date, year, and time. Date command gets the date of the operating system.
User Manual Date Command
To know more about the date-related commands, a manual guide is available for the user. The output shows the following description.
Get Dates in Different Formats
Dates can be displayed in more than one alternative method. Key “D” will display the date in the “/” form having a date, month, and year. Whereas “F” will show the date with the help of ”-“ and the format will have year, month, and day, respectively. To make it precise and easy for the user, we have used a string that shows the proper meaning of the format specifier, i.e., f and d. “+%” operator is used to bind the format specifier with the date.
Get Day, Month, and Year
There are some format specifiers used in the command with a date to enhance functionality. These are also shown in the user manual we have seen in the example above. For instance, we are going to explain some examples to make them easier for users to recognize. The syntax for format specifiers is:
As the syntax understands it, we use date and specified format to get a particular output.
Some specifiers are as follow:
%B= Displays full month name
%m= Displays the month of the year
%Y= Display the year
%T= Display the time
%H= Depicts the hour in time
%M= Display the minute in time
%S= Presents the seconds in time
The Month of the Year
To find the month of the year. We use the ”B” format as described above in examples.
Year To display the current year, we use the ”Y” format. If we only want the last two digits of the year, then a small “y” is used.
$ (date +%Y)
Day of the weekWe will use ”A” to get the full name of the day in the week. While “a” is used to get the abbreviation of a day.
$ (date +%a)
Get Dates of Past and Future
Last dayBy using this appended command, we will get yesterday’s date.
Particular Day Ago
We can get dates by providing a relevant date to go back to that particular date. For example, the user wants to get the date of 45 days ago, so this command is used.
The last month command will help the user to get the previous month of the current year.
Next year’s command shows the year after the current year.
Difference Between Dates
A definite day is obtained by providing two dates. One date is subtracted from the other so that the number of days is obtained between the two.
Display Date with –d or –date Format
Date command knows to handle the –d or –date input. It takes an input date as a string. To obtain the date on the next day of the coming week, we input the below example.
Next, the date command can calculate the seconds of the date that have passed till now. We provide the relative date, whose seconds are to be known.
Irrespective of the format we provided in the command, the date is always displayed in a particular pattern set as default.
Display Week Number in the Current Year
As shown in the command below, we can get the week number of the current year using “V”.
Day Number in the Current Year
Day number till the current date is displayed by using %j%.
Set Date According to Timezone
To check the current time zone of the system, we use the command below.
This will display the output of the date and local time of a specific time zone. Here the time zone is Asia/Karachi.
To change the given time zone to another, you need to know the name of the timezone. The format followed to display timezone is Region/City. Here are the available options of timezone:
The same query is used to change the time zone besides the specific time zone you want to replace.
In the above-mentioned output, you can see that the time zone is updated with Europe/Istanbul.
Get Date and Time by Setting the Hardware Clock
Hardware clocks work even if there is no power supply. It runs inside the hardware of the system.
The output will be demonstrated as shown in the image:
Now, we can set a date or change it through the command line in Linux. It’s quite easy and understandable. The queries mentioned above are discussed in detail for the users. Format specifiers help in setting the date of a particular day.