date and hwclock commands

Linux maintains two clocks: the hardware clock and the software clock. The battery driven hardware clock maintains the time while the computer is turned off. During the boot, Linux reads the hardware clock and sets the software clock to the value it retrieves.

You can manually set the software clock with the date command. Used without any options, this command prints the current time:

linux date command

By default, this command shows the time in local time. If you want to display the time in UTC, use the -u option:

linux date command utc

To set the software clock to a specific time, you need to specify a month, a day, an hour, and a minute at a minimum (in two-digit codes – MMDDhhmm). You can optionally add a 2– or 4-digit year. The time should be specified in the 24-hour format. For example, to set the time to September 16, 2014, 7:55 p.m., we would use the following command:

linux set date and time

09 – month
16 – day
19:55 – time
2014 – year

The command above specifies the time in local time. To set the clock in UTC, use the -u option.

 

You can also use the date command to display a future or past date. For example, to display the date of next Tuesday, use the following command:

date –date=”next tue”

linux date next tuesday

To show the date and time of four seconds ago, use the following command:

date –date=”4 seconds ago”

linux date seconds ago

To show the date and time of two months ago, use the following command:

date –date=”2 month ago”

linux date months ago

To show the date and time of two years ago, use the following command:

date –date=”2 year ago”

linux date years ago

You can combine arguments together. To find the date and time of two years and 50 days ago, you can use the following command:

date –date=”2 years ago 50 days ago”

linux date years and days ago

hwclock command

The date and time of the hardware clock is usually changed in the BIOS, but you can use the hwclock command to view and set the hardware clock. Used without any options, this command will display the date and time of your system’s hardware clock:

linux hwclock command

To set the hardware clock manually, you need to use two options: –set and –date. After the –date option, you need to specify the new date and time. For example, to set the hardware clock to September 12th, 2014 at 20:32:45, you would use the following command:

hwclock –set –date “09/12/2014 20:32:45”

linux hwclock set date

You can also take the output of the date command and pass it as the argument, as shown below:

linux hwclock date command output

To set the hardware clock based on the software clock, use the -w or –systohc option:

linux hwclock software clock

To set the software clock based on the hardware clock, use the -s or –hctosys option:

linux hwclock set software clock

Subscribe to our Newsletter

Like us on Facebook

top
Geek University 2019