Udemy logo

linuxsetdateLinux is a free, open source operating system. It’s widely used by computer engineers and the technical community due to this reason. It is also a command line based operation system, and thereby gives the end user a much finer level of control.

The Linux date instruction is a versatile command that shows the date and time that your system is presently set to. It also lets you set or change the time and date, and select what date/time format you want to use. In today’s tutorial, we will learn about the basic commands, to set the date and time in your Linux.  You do not really need prior LInux experience, but we do recommend you do a quick Linux crash course before you proceed. This will enable you to relate better to the commands we’ll be using.

It is important that the system date be set correctly, since it is used by various applications that run on your computer. It is very common to have the date in your system either set wrong, or the clock set on an incorrect time zone. So, you will have to set the date from the command line.

See the current date and time

Using date without any options will print out the present date and time in the default format. You can format the output to show the clock in any way you like. Some options are mentioned below:

%H: hour

%M: minutes

%S: seconds

%D: date equals to %m/%d/%y

%Z: timezone

%j: day of the year

Bash$ date-u+”Time is % H % M % S on % D in % Z(% j day of the year)”
Time is 05:26:45 on 1/27/14 in UTC(265 DAY OF THE YEAR)

Using the –u option will print the date and time in the coordinated universal time(UTC).

You can read date patterns from your file with the – file option. You can do it for multiple date strings. You can also display past date by using the – date command.

$ date – date =”yesterday”
Sun May 20 21:47:56 PDT 2013

Setting the Time

The date command can be used to set the time to whatever you want. So, in order to set the time to 5 hours 46 minutes and 50 seconds in the evening (PM), you can use the following

Bash $ date – s 17:46:50

The colon (:)will acts as the default delimiter for the time. Both time and date can be set in a single command.

Bash$ date – s “27 Jan 2014 17:46:50”

The above command will set the present time to 17:46:50 and the date to 27 jan 2014.

Setting the Date

The date command can also be used to modify and set the system date. The set option (-s) lets you do this. For example, to set the date to 27 Jan 2014, you can do the following steps:

Bash$ date –s “27 Jan 2014”

Hardware Clock

Other than the system date and time, there is also a system hardware time. This time is usually maintained by hardware with the support of an inbuilt lithium battery independent of OS and other factors.

The system clock and hardware clock are independent. They both have to be set separately by the Linux command. To show the hardware clock.

Bash$ hw clock- show
Tue Oct 9 01:16:34 2012 – 0.65748 seconds.

If there is the difference in the time between the system clock and hardware clock, then you can easily set your hardware clock to match the system clock with the – systohc option.

Bash$hwclock- systohc.

All the above mentioned methods will permit you to set and alter the system date and time. If you want to keep the system clock accurate automatically at all times, then you have to use an internet time server. The Network Time Protocol can be installed as a Linux daemon which will link to a network server to adjust and update the time periodically.

Setting your timezone

Some files use local time, with a link to a separate file containing information about your time zone. Zone information files are commonly in /usr /share/ zoneinfo, but this position depends upon the distribution of files. So, in this case, if your localtime file indicates to a zone info file other than your file zone then you can modify it by browsing the directories in order to locate your city in the same time zone by in / usr /share/ zoneinfo and then link local time to it.

Bash$ in – sf / usr / share / zoneinfo / India / New Delhi / etc / localtime

Some applications can use the configuration file /etc /sysconfig / clock to determine the present time zone so, it is always advisable to set the ZONE entry.

As always, you can use Linux’s inbuilt help for quick info or to check all command line options. To do this, use the following command

Bash$ man date

Of course, to get a holistic understanding of Linux, we recommend you try out this LInux crash course. Once you are comfortable with that you can move onto to mastering the linux command line. Perhaps you can even graduate to setting up your own Linux server!

Page Last Updated: January 2014

Empower your team. Lead the industry.

Get a subscription to a library of online courses and digital learning tools for your organization with Udemy Business.

Request a demo