Setting up Linux Date and Time

Posted on 9:17 PM by Bharathvn

The date command is the primary command-based interface for viewing and changing
date and time settings, if you are not having that done automatically with NTP.
Here are examples of date commands for displaying dates and times in different ways:

$ date Display current date, time and time zone
Sun Aug 12 01:26:50 CDT 2007

$ date ‘+%A %B %d %G’ Display day, month, day of month, year
Sunday August 12 2007

$ date ‘+The date today is %F.’ Add words to the date output
The date today is 2007-08-12

$ date --date=’4 weeks’ Display date four weeks from today
Sun Sep 9 10:51:18 CDT 2007

$ date --date=’8 months 3 days’ Display date 8 months 3 days from today
Tue Apr 15 10:59:44 CDT 2008

$ date --date=’4 Jul’ +%A Display day on which July 4 falls
Wednesday

Although our primary interest in this section is time, since we are on the subject
of dates as well, the cal command is a quick way to display dates by month. Here are
examples:

$ cal Show current month calendar (today is highlighted)
August 2007
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 2007 Show whole year’s calendar
2007
January February March
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 3 4 5 6 1 2 3 1 2 3
7 8 9 10 11 12 13 4 5 6 7 8 9 10 4 5 6 7 8 9 10
14 15 16 17 18 19 20 11 12 13 14 15 16 17 11 12 13 14 15 16 17
21 22 23 24 25 26 27 18 19 20 21 22 23 24 18 19 20 21 22 23 24
28 29 30 31 25 26 27 28 25 26 27 28 29 30 31
...

$ cal -j Show Julian calendar (numbered from January 1)
August 2007
Sun Mon Tue Wed Thu Fri Sat
213 214 215 216
217 218 219 220 221 222 223
224 225 226 227 228 229 230
231 232 233 234 235 236 237
238 239 240 241 242 243

The date command can also be used to change the system date and time. For example:

$ sudo date 081215212008 Set date/time to Aug. 12, 2:21PM, 2008
Tue Aug 12 11:42:00 CDT 2008

$ sudo date --set=’+7 minutes’ Set time to 7 minutes later
Sun Aug 12 11:49:33 CDT 2008

$ sudo date --set=’-1 month’ Set date/time to one month earlier
Sun Jul 12 11:50:20 CDT 2008

The next time you boot Ubuntu, the system time will be reset based on the value of
your hardware clock (or your NTP server, if NTP service is enabled). And the next
time you shut down, the hardware clock will be reset to the system time, in order to
preserve that time while the machine is powered off. To change the hardware clock,
you can use the hwclock command.