home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Complete Idiot's Guide to Linux
(Publisher: Macmillan Computer Publishing)
Author(s): Manuel Ricart
ISBN: 078971826x
Publication Date: 12/22/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Viewing and Setting the Date and Time: date

The date command allows you to read the current date and time as well as to set the time for your system (if you runt it as root). It provides a multitude of options that allow you to use a variety of formats for the date and time.

By default, if you enter the date command, it prints the current date and time. To format the date, provide the + argument followed by a formatting string. The various options for the formatting strings (shown in Table 17.1) are preceded by a percent (%) symbol.

Table 17.1 Date Options

Option Description

%H Print hour in 24-hour format with leading zeros (00..23)
%I Print hour in 12-hour format with leading zeros (01..12)
%k Print hour in 24-hour format (0..23)
%l Print hour in 12-hour format (1..12)
%M Print minutes (00..59)
%p Print AM or PM
%r Print time in 12-hour format with AM/PM (hh:mm:ss AM)
%s Print the number of seconds since 1/1/70
%S Print second (00..61)
%T Print time in 24-hour format (hh:mm:ss)
%X Print time in locale’s time representation (%H:%M:%S)
%Z Print time zone (e.g., CST)
%a Print locale’s abbreviated weekday name (Sun)
%A Print locale’s full weekday name, variable length (Sunday)
%b Print locale’s abbreviated month name (Jan)
%B Print locale’s full month name (January)
%d Print day of month (01..31)
%D Print date as mm/dd/yy
%j Print the number of the day in the year (001..366)
%m Print the month (01..12)
%U Print week number of year (00..53)
%w Print day of week (0..6), with 0 corresponding to Sunday
%W Print week number of year with Monday as first day of week (00..53)
%x Print locale’s date representation (mm/dd/yy)
%y Print last two digits of year (00..99)
%Y Print the year using 4 digits (1970...)

For example, to print the date in long format, you would type this:

     [alberto@digital alberto]$ date +”%A %B %d %Y”
     Sunday September 20 1998

Note that the format string, %A %B %d %Y, is enclosed by quotation marks (). (Enclosing the string in quotes is necessary so that the shell doesn’t think it is receiving four separate arguments.) When you enclose the string in quotes, you can then add any text as separators inside of the string. For example, to add a comma after the day, you would type this:

     [alberto@digital alberto]$ date +”%A, %B %d %Y”
     Sunday, September 20 1998

To print the date as a number using the pattern YYYYMMDD, do this:

     [alberto@digital alberto]$ date +%Y%m%d
     19980920

To set the system clock, you would provide a date in the following format:

date MMDDhhmmYYYY
MM is a two-digit number for the month (01–12)
DD is a two-digit number for the day (01–31)
hh is a two-digit hour (in the 24-hour format, 00 is midnight, 23 is 11 PM)
mm is a two-digit minute (00–59)
YYYY is a four-digit year (1998)

Getting a Calendar: cal

The cal command prints a calendar. By default, it prints a calendar for the current month:

     [alberto@digital alberto]$ cal
        September 1998
     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

If you provide a single argument, it is assumed to be a year:

     [alberto@digital alberto]$ cal 1998
                                    1998

            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    1  2  3  4  5  6  7    1  2  3  4  5  6  7
      4  5  6  7  8  9 10    8  9 10 11 12 13 14    8  9 10 11 12 13 14
     11 12 13 14 15 16 17   15 16 17 18 19 20 21   15 16 17 18 19 20 21
     18 19 20 21 22 23 24   22 23 24 25 26 27 28   22 23 24 25 26 27 28
     25 26 27 28 29 30 31                          29 30 31

             April                   May                   June
     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                   1  2       1  2  3  4  5  6
      5  6  7  8  9 10 11    3  4  5  6  7  8  9    7  8  9 10 11 12 13
     12 13 14 15 16 17 18   10 11 12 13 14 15 16   14 15 16 17 18 19 20
     19 20 21 22 23 24 25   17 18 19 20 21 22 23   21 22 23 24 25 26 27
     26 27 28 29 30         24 25 26 27 28 29 30   28 29 30
                            31

             July                  August                September
     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                      1          1  2  3  4  5
      5  6  7  8  9 10 11    2  3  4  5  6  7  8    6  7  8  9 10 11 12
     12 13 14 15 16 17 18    9 10 11 12 13 14 15   13 14 15 16 17 18 19
     19 20 21 22 23 24 25   16 17 18 19 20 21 22   20 21 22 23 24 25 26
     26 27 28 29 30 31      23 24 25 26 27 28 29   27 28 29 30
                            30 31

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

Note that cal 98 and cal 1998 will print very different calendars, as 98 and 1998 are very different years.

To display a particular month in a year, provide two arguments. The first is taken as the month, the second as the year:

     [alberto@digital alberto]$ cal 12 1998
         December 1998
     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


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.