We use the date command without any arguments to display the system date and time. When I need to change the system time, I cannot remember the syntax at all.
I resort to google but I land on a page with multiple examples and it can be confusing especially in the heat of the moment. I don’t need 10 examples, I just need one.
This is how I have decided to remember the syntax. Run “man date” and at the top, we see this format:
CC: First two digits of Year
YY: Last two digits of year
Here is an example
$ sudo date 040709302017.00 Fri Apr 7 09:30:00 UTC 2017
will set the time to April 07, 09.30 AM, 2017 and 0 seconds. If that is all you need, stop here and get back to work.
Note that I excluded the option to set it to utc to keep this post simple.
As in any other Linux command syntax, we can omit the options inside brackets. Running just date displays the date and time. Hence the minimum command we need to set time is “date MMDDhhmm” will set the given month, day, hour and minute.
$ sudo date 04070930 Fri Apr 7 09:30:00 UTC 2017
Year[[CC]yy] and Second[.ss] are optional. To include year, we can use 4 digits (2017) or just the last two digits (17). Example:
$ sudo date 0407093017 Fri Apr 7 09:30:00 UTC 2017
The other popular format which I cannot remember is:
date -s "STRING"
Where STRING can be in various forms such as:
“Mon, 07 Apr 2017 09:21:42 -0800”