The only date command format you need to know in Linux

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:

date  [MMDDhhmm[[CC]YY][.ss]]

Where

MM: Month
DD: Day
hh: Hour
mm: Minute
CC: First two digits of Year
YY: Last two digits of year
.ss: seconds

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”
“2017-04-97 09:21:42”
etc.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s