Linux date command - Display and Set System Date and Time

date linux command

Date on the operating system may only be considered as a timepiece. Especially on console mode, we do generally not see date as an important think. But for Administrator, this assumption is false. Do you know that a wrong date and time can make you can’t compile an application?

Because date and time is important, this is may be the reason why Network Time Protocol is developed. Let’s start to see what date command do for you.

Display system date

To display your system date, just type

$ date
Thu Dec 5 22:55:41 WIB 2013

Formatting Date

Date come with many formats. If you are unhappy with default format you can change it. You may think “Why I need to change the format? A default output is enough for me”.
Yes. It is true. But when you do programming, default output may not meet the user need. So here’s some custom outputs.

Output date and time in RFC 2822 format

$ date -R
Thu, 05 Dec 2013 23:40:53 +0700

RFC 2822 has a format like this : day, date-month-year, hours:minutes:second timezone
Timezone +0700 is same with GMT +7
By default date is using the timezone which defined in /etc/localtime. Valid timezones data are defined in /usr/share/timezones

Print or set Coordinated Universal Time

From Wikipedia, UTC means

The primary standard which the world regulates clocks and time. It is one several closely related successors to Greenwich Mean Time.

To display your date and time with UTC format, use -u parameter

$ date -u
Thu Dec 5 16:45:58:UTC 2013

Using formatting options

To custom your date format, use a plus sign (+)

$ date +”Day : %d Month : %m Year : %Y”
Day: 05 Month: 12 Year: 2013

$ date +%D

%D format follows Year/Month/Day format.

You can also put the day name if you want. Here’s some examples :

$ date +”%a %b %d %y”
Fri 06 Dec 2013

$ date +”%A %B %d %Y”
Friday December 06 2013

$ date +”%A %B %d %Y %T”
Friday December 06 2013 00:30:37

$ date +”%A %B-%d-%Y %c”
Friday December-06-2013 12:30:37 AM WIB

There are still a lot of format options available. Just type

$ date --help


$ man date

To show date command syntax and parameters.

So basically, date command will interpret all percent sign (%) and print anything inside a quotes sign (“ “)

Set system date and time

Generally, you want your system date and time is set automatically. If for some reason you have to change it manually, we can use this command :

# date --set=”20140125 09:17:00”

It will set your current date and time of your system into January 25, 2014 and 09:17:00 AM. Please note, that you must have root privilege to do this. Otherwise you will have an error message like this :

date: cannot set date: Operation not permitted
Sat Jan 25 09:17:00 WIB 2014

Reset your time back

If you need to reset your system date and time back to the original, you can do this trick.

# hwclock
Fri 06 Dec 2013 03:44:10 AM WIB -0.314082 seconds

And set your system date and time to the output of hwclock command.

Using date command on a script

Remember when I said before about why you may need to change the date output? One of the answer may be because you do programming. Let’s see an example on bash script.

$ vi

#! /bin/bash
DATETIME=$(date +”DATE: %a %b-%d-%Y TIME: %T WEEK NUMBER: %W”)

Save it and run it using :

$ ./
DATE : Fri Dec-06-2013 TIME: 03:08:19 WEEK Number :40

If you find error permission denied error message, type :

$ chmod 755

Using date on a backup procedure

Another example is when you are using date on a backup procedure.

$ date +%F

$ tar zcfv /daily_backup/backup-`date +%F`.tar.gz /home/pungki/Documents

It will compress folder /home/pungki/Documents into a a file with name backup-2013-12-06.tar.gz which located in /daily_backup folder.


Date may be seen as something that is not important. But dates play an important role. As usual, to have more detail in using date command, please visit date manual page by typing man date in your console.


Share This :

Free Linux Ebook to Download

Comments (3)

Trackback URL | Comments RSS Feed

  1. Jan says:

    The time and date has disappeared from my desktop. Normally, it appears to the upper far right hand side of my screen. Your instructions are confusing and I can't complete them to display the time and date automatically on start-up and through out my computer session. Help!

    • Bobbin Zachariah says:

      Article was focusing on setting date from command line. By the way which linux distro you use ? Was it working before ?
      Is date command work from terminal ?

  2. Terion says:

    Doesn't work correctly on Debian. I think somehow the system is still using network time, even though I disabled it and can't find a running NTP client. I was trying to mess around with the date, but this happens:

    sudo date -s "$( date --rfc-3339="seconds" | sed s/2016/2017/ )"; date; sleep 1; date
    Thu 12 Jan 14:43:59 CET 2017
    Thu 12 Jan 14:43:59 CET 2017
    Tue 12 Jan 14:44:00 CET 2016

    After a single second, the date gets corrected to the real date.

Leave a Reply

Commenting Policy:
Promotion of your products ? Comment gets deleted.
All comments are subject to moderation.