Setting the time and date in Linux

Working with times and dates in Linux is a relatively easy task. Most GUI interfaces offer you a point and click method to update the time and date but what about the command line interface (CLI)?

Get the current time and date Linux

Well the first thing we need to do if get the current time and date to see if its correct or not, we can do this with the following command:

$ date
Mon Oct 27 11:52:28 UTC 2014

If you just want the time you can use the following command:

$ date %T
16:04:21

What if you wanted the time in a 12h format? Well thats also simple by passing %r:

$ date +”%r”
04:05:42 PM

Cool, we are getting some valuable information now, what about getting the month?

$ date +”%B”
October
$ date +”%b”
Oct

Setting the time and date in Linux

So if your time and/or date is wrong lets set it to the correct time and date you can do this a number of ways, here are a few examples:

$ date 102716142014.15

The above sets the date to Oct 27th 2014, 16:14 and 15 seconds. It takes arguments as follows:

date MMDDHHMIYYYYSS

MM - Month
DD - Date
HH - Hour (24 hour clock)
MI - Minutes
YYYY - Year
SS - Seconds

There are a few others ways you can set the time and date in Linux:

$ date -s “10/27/2014 16:14:15”

$ date -s “27 OCT 2014 16:14:15”

$ date +%Y%m%d -s “20141027”

What if you wanted to just set the time? Well you can do that with the following:

$ date +%T -s “16:14:15”

Or if you want to set the time in 12h format:

$ date +%T%p -s “04:14:15PM”

As you can see there's many different ways you can set the time and date and also retrieve the information from the Linux command line which is very helpful when setting up a new server or creating shell scripts.

Leave a Reply

Your email address will not be published. Required fields are marked *