To set or display the time in Linux can be achieved with the date program from the command line. To set the time you are required to use sudo or run as root. To display the time you can be running as any user. I must admit, I always have had to lookup how to format the date or how to set it as it is somewhat annoying. Once you figure it out though, its a piece of cake. In fact, the format strings are quite an ingenious idea as long as you can remember what they are!

To display or set the time you need to (can) format the output as needed. Then you must decide whether you want to simply display the date, or if you want to modify the time. So:

  1. Decide the format to use from piecing together day, month, year, hour, minute, etc. as well as timezone.
  2. Decide whether you need to just output the date, or set using the -s command.

Format Options For ‘date’ Command

Well this means we need to take a look at the formatting options available to us, it is a lengthy list but of importance. Thanks date –help:

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
  %%   a literal %
  %a   locale's abbreviated weekday name (e.g., Sun)
  %A   locale's full weekday name (e.g., Sunday)
  %b   locale's abbreviated month name (e.g., Jan)
  %B   locale's full month name (e.g., January)
  %c   locale's date and time (e.g., Thu Mar  3 23:05:25 2005)
  %C   century; like %Y, except omit last two digits (e.g., 20)
  %d   day of month (e.g, 01)
  %D   date; same as %m/%d/%y
  %e   day of month, space padded; same as %_d
  %F   full date; same as %Y-%m-%d
  %g   last two digits of year of ISO week number (see %G)
  %G   year of ISO week number (see %V); normally useful only with %V
  %h   same as %b
  %H   hour (00..23)
  %I   hour (01..12)
  %j   day of year (001..366)
  %k   hour ( 0..23)
  %l   hour ( 1..12)
  %m   month (01..12)
  %M   minute (00..59)
  %n   a newline
  %N   nanoseconds (000000000..999999999)
  %p   locale's equivalent of either AM or PM; blank if not known
  %P   like %p, but lower case
  %r   locale's 12-hour clock time (e.g., 11:11:04 PM)
  %R   24-hour hour and minute; same as %H:%M
  %s   seconds since 1970-01-01 00:00:00 UTC
  %S   second (00..60)
  %t   a tab
  %T   time; same as %H:%M:%S
  %u   day of week (1..7); 1 is Monday
  %U   week number of year, with Sunday as first day of week (00..53)
  %V   ISO week number, with Monday as first day of week (01..53)
  %w   day of week (0..6); 0 is Sunday
  %W   week number of year, with Monday as first day of week (00..53)
  %x   locale's date representation (e.g., 12/31/99)
  %X   locale's time representation (e.g., 23:13:48)
  %y   last two digits of year (00..99)
  %Y   year
  %z   +hhmm numeric timezone (e.g., -0400)
  %:z  +hh:mm numeric timezone (e.g., -04:00)
  %::z  +hh:mm:ss numeric time zone (e.g., -04:00:00)
  %:::z  numeric time zone with : to necessary precision (e.g., -04, +05:30)
  %Z   alphabetic time zone abbreviation (e.g., EDT)
 
By default, date pads numeric fields with zeroes.
The following optional flags may follow `%':
 
  -  (hyphen) do not pad the field
  _  (underscore) pad with spaces
  0  (zero) pad with zeros
  ^  use upper case if possible
  #  use opposite case if possible

Now that we got that out of the way, and you have memorized it we can move on. I will show you how to use the above to format the date output.

Display Date Using Formatting Options

Okay, lets do some outputting with the different formatting options. The beauty of this is we can throw in symbols between the day month year or however many fields you want to output, so if you were parsing it with sed you could split on : for example.

Output With Different Formats

1
2
3
4
$ date +%d:%m:%y
10:03:11
$ date +%d:%-m:%y
10:3:11

So referring back to the format strings, I grabbed the:

%d day of month (e.g, 01)
%m month (01..12)
%y last two digits of year (00..99)

Notice in the second output, I removed the “03″ to be “3″, if you add in the hypen then it removes any padding, this can be applied to any format specifier. A list of examples:

Note: the // is a comment added into the HTML so you get the definition of the format I used.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$ date +%x  // %x   locale's date representation (e.g., 12/31/99) 
11-03-10
$ date +%X  // %X   locale's time representation (e.g., 23:13:48)
09:50:50 AM
$ date -u   // -u, --utc, --universal    print or set Coordinated Universal Time
Thu Mar 10 17:50:56 UTC 2011
$ date +%D  // %D   date; same as %m/%d/%y
03/10/11
$ date +%Y  // %Y   year
2011
$ date +%Z  // %Z   alphabetic time zone abbreviation (e.g., EDT)
PST
$ date +%N  // %N   nanoseconds (000000000..999999999)
618868208
$ date +%V  // %V   ISO week number, with Monday as first day of week (01..53)
10

Wooo, well that’s quite handy. But I want to set the time, not display it! Here is how we put it all together, now that formatting hopefully makes some sense?

Setting The Time With ‘date -s’

To modify the system time you must be running with sudo, or as root. So lets say you just want to set the time of day, and not the entire date. Notice also how we specify our format, then use the -s command to say hey, lets actually SET or time.

1
2
3
4
5
6
7
8
9
10
# date
Thu Mar 10 10:07:45 PST 2011
# date +%H%M -s "920"
0920
# date
Thu Mar 10 09:20:01 PST 2011
# date +%H%M -s "1008"
1008
# date
Thu Mar 10 10:08:02 PST 2011

In the example above, you can see how we are telling date the format we want to use to set the time. Pretty cool! How about if we want to change the month and year? Just use the format you feel comfortable with! Here comes:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# date
Thu Mar 10 10:08:02 PST 2011
# date +%Y%m%d 
20110310
# date +%Y%m%d -s "19831103"
19831103
# date
Thu Nov  3 00:00:01 PST 1983
# date +%Y%m%d -s "20110310"
20110310
# date
Thu Mar 10 00:00:01 PST 2011
# date +%T -s "10:19:12"
10:19:12
# date
Thu Mar 10 10:19:12 PST 2011

Notice once I set the year with no time values, it defaults to the 0 hour, 0 minute, and 0 second; thus I also had to set the time. So that is how you can set time and date using the format specifiers. Now you should always be on time!