Set Date in Linux from the Command Line

13

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.

Prerequisites

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.

Date Syntax

$ date [OPTION][+FORMAT]

Date

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.

Set Date in Linux from the Command Line 1619313653 132 Set Date in Linux from the Command Line

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.

Set Date in Linux from the Command Line 1619313653 858 Set Date in Linux from the Command Line

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.

Set Date in Linux from the Command Line 1619313654 399 Set Date in Linux from the Command Line

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:

$date + % [format-option]

As the syntax understands it, we use date and specified format to get a particular output.

Some specifiers are as follow:

%d= Presents the day of the month

%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.

Set Date in Linux from the Command Line 1619313654 633 Set Date in Linux from the Command Line

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.

Set Date in Linux from the Command Line 1619313654 666 Set Date in Linux from the Command Line

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)

$ (date +%a)               

Set Date in Linux from the Command Line 1619313655 330 Set Date in Linux from the Command Line

Get Dates of Past and Future

Last dayBy using this appended command, we will get yesterday’s date.

$ Echo “yesterday= $(date –d “yesterday”)

Set Date in Linux from the Command Line 1619313655 973 Set Date in Linux from the Command Line

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.

$ Echo “before 45 days =$(date –d “tomorrow -45 days”)

Set Date in Linux from the Command Line 1619313656 380 Set Date in Linux from the Command Line

Last Month

The last month command will help the user to get the previous month of the current year.

$ Echo “last month= $ (date –d “last month” “+%B”)

Next Year

Next year’s command shows the year after the current year.

$ Echo= “next year =$ (date –d “next year” +%Y”)

Set Date in Linux from the Command Line 1619313656 612 Set Date in Linux from the Command Line

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.

$ Echo $((($(date –d “2021324” “+%s”) – $(date –d “2021318” “+%s”))/86400))

Set Date in Linux from the Command Line 1619313656 492 Set Date in Linux from the Command Line

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.

$ datedate=”next Tuesday”

Set Date in Linux from the Command Line 1619313657 483 Set Date in Linux from the Command Line

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.

$ date –d “2021424” +”%s”

Set Date in Linux from the Command Line 1619313657 60 Set Date in Linux from the Command Line

Irrespective of the format we provided in the command, the date is always displayed in a particular pattern set as default.

$ date –d “5/6/2021

Set Date in Linux from the Command Line 1619313658 867 Set Date in Linux from the Command Line

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”.

Set Date in Linux from the Command Line 1619313658 183 Set Date in Linux from the Command Line

Day Number in the Current Year

Day number till the current date is displayed by using %j%.

Set Date in Linux from the Command Line 1619313658 968 Set Date in Linux from the Command Line

Set Date According to Timezone

To check the current time zone of the system, we use the command below.

Set Date in Linux from the Command Line 1619313659 598 Set Date in Linux from the Command Line

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:

$ timedatectl list-timezones

Set Date in Linux from the Command Line 1619313659 307 Set Date in Linux from the Command Line

The same query is used to change the time zone besides the specific time zone you want to replace.

$ sudo timedatectl set –timezone Europe/Istanbul

Set Date in Linux from the Command Line 1619313660 933 Set Date in Linux from the Command Line

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:

Set Date in Linux from the Command Line 1619313660 755 Set Date in Linux from the Command Line

Conclusion

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.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More