Linux Commands

Set Date in Linux from the Command Line

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.

$ date

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.

$ man date

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:

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

$ (date +%B)

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)

$ (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)

$ (date +%a)               

Get Dates of Past and Future

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

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

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

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

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 “2021-3-24” “+%s”) - $(date –d “2021-3-18” “+%s”))/86400))

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”

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 “2021-4-24” +”%s”

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

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

$ Date +%j

Set Date According to Timezone

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

$ Timedatectl

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

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

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.

$ sudo hwclock –show

The output will be demonstrated as shown in the image:

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.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.