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:
By default, this command shows the time in local time. If you want to display the time in UTC, use the -u option:
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:
09 – month
16 – day
19:55 – time
2014 – year
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”
To show the date and time of four seconds ago, use the following command:
date –date=”4 seconds ago”
To show the date and time of two months ago, use the following command:
date –date=”2 month ago”
To show the date and time of two years ago, use the following command:
date –date=”2 year 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”
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:
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”
You can also take the output of the date command and pass it as the argument, as shown below:
To set the hardware clock based on the software clock, use the -w or –systohc option:
To set the software clock based on the hardware clock, use the -s or –hctosys option: