Linux.com

Home Learn Linux Linux Tutorials How to Change the Linux Date and Time: Simple Commands

How to Change the Linux Date and Time: Simple Commands

Telling the time on Linux is more complicated than it might seem at first glance. To start with, the time command on Linux doesn't tell the time:

Prague clock$ time 
real 0m0.000s
user 0m0.000s
sys 0m0.000s

Because time is a timer for measuring how long a process runs. For example, how long does it take to recursively list all the files in a directory?

$ time ls -Rl dir/* 
[...]
real 0m22.156s
user 0m1.652s
sys 0m4.772s

Date for Time

It may sound odd, but you must use the date command to see the time on Linux:

$ date 
Thu Oct 11 11:47:25 PDT 2012

The date command is fundamental to understanding time options on Linux. For example, the panel clock in Xfce4 supports using the standard date options to customize the date and time display. Figure 1 shows what mine looks like.

Telling time in Linux

This comes from these FORMAT options for the date command: %r %n%a %b %d, %Y, which you can easily test for yourself:

$ date +"%r %n%a %b %d, %Y" 
12:05:00 PM
Thu Oct 11, 2012

man date details all the formatting options. Note how you can use ordinary spacing and punctuation to control the appearance. You customize date and time displays to suit your own whims, and in consistent, script-friendly ways.

man date lists a good set of options, but it does not tell you everything. To get the complete story of date you need the GNU coreutils manual. And that is where we learn about the magic strings that let us ask for dates next week, last year, day of week, and many more. Like the date three Tuesdays from now, five months from now, eight years ago:

$ date -d "third tuesday" 
Tue Oct 30 00:00:00 PDT 2012
$ date -d "fifth month"
Mon Mar 11 14:02:54 PDT 2013
$ date -d "8 years ago"
Mon Oct 11 14:03:32 PDT 2004
$ date -d "23 years ago 2 months 19 days 17 hours 59 minutes"
Sun Dec 31 06:48:14 PST 1989

You can quickly check the time in a different time zone:

$ date +"%r EST" 
01:53:10 PM EST

UTC (Coordinated Universal Time) is the universal standard for time worldwide. When you know your UTC offset you always know what time it is, because date will tell you:

$ date -R 
Thu, 11 Oct 2012 13:56:17 -0700

If you live in one of those demented regions that uses Daylight Savings Time, date -R will always tell you the correct offset no matter what time of year it is.

Cal For Dates

When you just want to see some dates, think of our good old friend cal:

$ cal     
October 2012
Su Mo Tu We Th Fr Sa
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31

cal -3 displays three months: last month, this month, and next month. cal YYYY displays a specific year, like cal 1962.

ncal is included on most Linux systems, and it is an oldtimer designed to fit nicely on a 25x80 terminal:

$ ncal    
October 2012      
Su     7 14 21 28   
Mo  1  8 15 22 29   
Tu  2  9 16 23 30   
We  3 10 17 24 31   
Th  4 11 18 25      
Fr  5 12 19 26      
Sa  6 13 20 27

ncal -b switches to our customary horizontal display. ncal will show an arbitrary number of months in the past or future. For example ncal -bB 6 displays the current month plus six months previous, and ncal -bA 6 display the current month plus the next six months.

You can see any month in any year, for example March 1950, with ncal -bm March 1950. This works for future months, too.

Those Wacky ls Timestamps

The way the ls outputs the date and time is a continual source of vexation because it varies on the different Linux distributions. This is how it looks on my Linux Mint system:

$ ls -l 
-rw-r----- 1 carla carla 11537 Oct 1 17:16 hp-check.log
-rw-r--r-- 1 carla carla 705 Aug 12 2011 ledproject.txt

Files dated within the last six months display the time instead of the year, and older files display the year and not the time. The Mint time style is called iso, and this is the the GNU default. long-iso is my preference, and it looks like this:

$ ls -l --time-style=long-iso 
-rw-r----- 1 carla carla 11537 2012-10-01 17:16 hp-check.log
-rw-r--r-- 1 carla carla 705 2011-08-12 12:15 ledproject.txt

I like long-iso because it's easy to sort-- year, month, day, single-digit months and days are padded to two digits, and it uses a 24-hour clock. This is all defined in ISO 8601. On Linux it's controlled by the TIME_STYLE environment variable, so you can override the default system-wide in /etc/profile, or in your personal .profile or .bashrc by adding a line like export TIME_STYLE=long-iso, then logging out and back in.

You might want to create a custom timestamp. This uses the same options as the date command, and you can test this on the command line before making it permanent in a configuration file:

$ export TIME_STYLE="+%Y-%m-%d %H:%M:%S %z" 
$ ls -l -rw-r----- 1 carla carla 11537 2012-10-01 17:16:45 -0700 hp-check.log
-rw-r--r-- 1 carla carla 705 2011-08-12 12:15:02 -0700 ledproject.txt

The GNU manual spells all this out in plain English. You can experiment to your heart's content, and then log out and log back in to reset to your system default.

And that is just the beginning of telling time on Linux. My dream is a lifestyle that doesn't need clocks at all, but I haven't figured out how to do that in Linux.

 

Comments

Subscribe to Comments Feed
  • woo Said:

    thank you very much for this clear article, i found it very useful and informative.

  • Eric Buissonnet Said:

    I had no idea date was so powerfull. Note that the ' GNU coreutils manual ' link is probably not the expected one

  • dw5 Said:

    The title of this article is 'How to Change the Linux Date and Time: Simple Commands'. You EXPLAIN the Linux Date and Time commands, but you never actually describe how to effectively CHANGE the actual time using the 'date' command. Therefore, IMHO, you should probably consider changing this article's title.

  • Cory Koch Said:

    Instead of logging in then out you to test you changes you can also just reload your configurations in your current tty by executing source /etc/profile and or source ~/.bashrc and or . /etc/profile . ~/.bashrc since . == source see bash man pages (or your preferred shell ) for more info.

  • Uncle Ed Said:

    Such a classy reply to the lady. I'm sure we're all impressed. I don't know why she has the negative feeling. One possibility is that its inconsistent implementation among different areas can have you missing a plane or appointment if the time source you're trusting isn't correct and up to date for the location.

  • cb Said:

    Great post and good refresh. Everyone needs to be reminded about the GNU Coreutils, thanks for providing a link to them in your post. There is a wealth of information there that is of benefit to all versions (distros) of Linux!

  • christian Said:

    Thank you linux because i easily use this program in our business.....

  • Sandeep Said:

    This is really very good article ... Some one had tried to write on this article too .. See if you get more info then ...http://roundoverlinux.blogspot.com/2012/09/date-command-in-linux.html

  • Chamraeun Said:

    Thanks you. But some commands not found in Red Hat

  • johnsonn Said:

    For Any Antivirus Tech Support Contact at: +1-844-833-8353

Upcoming Linux Foundation Courses

  1. LFS230 Linux Network Management
    06 Oct » 09 Oct - Virtual
    Details
  2. LFS416 Linux Security
    06 Oct » 09 Oct - Washington
    Details
  3. LFD331 Developing Linux Device Drivers
    13 Oct » 17 Oct - Virtual
    Details

View All Upcoming Courses

Become an Individual Member
Check out the Friday Funnies

Sign Up For the Linux.com Newsletter


Who we are ?

The Linux Foundation is a non-profit consortium dedicated to the growth of Linux.

More About the foundation...

Frequent Questions

Join / Linux Training / Board