Working With The Calendar Function In Terminal 0

Terminal

Share
Working With The Calendar Function In Terminal

Hey

Today and tomorrows post will be a two part mini-series on the date and calendar function within Terminal on your Mac. If you are ever into scripting or working with GeekTool (I have an old post about GeekTool here) it may be very useful to learn how to work with the calendar function in various scripts or what ever you decide to use it with. The calendar function is very useful for producing graphical representations (in text) of any month of any year. Tomorrows post will feature the date function which complements the calendar function nicely. The commands talked about in post can be found (along with more commands) in a variety of books such as Mac OS X Toolbox. Any basic Unix book will feature these basic commands.

To start open Terminal found in Applications > Utilities and type cal (then hit enter) into the command line. Your Terminal will produce something similar to the following.

    January 2010
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

A nice representation of the current month. This is very useful for graphical representations. You can also use ncal. Its a variation of cal, but produces a different output. Some of the commands either use cal, or ncal.

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

Notice with the ncal output its more square a regular. It does however read in a different direction which can make it slightly more difficult to look and glance at. However ncal does bring with it a couple more features which will be discussed later.

To change the month shown simply type:

cal -m apr

and the month of the current year will be displayed. The month format is the 3 letter short version; jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec. You can then combine this with a year, anything from 0AD to 9999AD. Minus month and years in the 10th millennium will not compute. For example

cal apr 2015

This will produce a calendar for April in the month 2015. Notice you don’t have to append the “-m” modifying option. This is so the function can tell when you are imputing a month and not confuse itself over a year with three letters. Note, if you enter the following:

cal 2015

or

cal -m apr -y 2010

It will list every day in that year regardless of the -m option. The same works with ncal.

There are a couple of options which can be added to the cal options (other than a specific date) to calculate which week or day in the year you are on. For example you can list each day sequentially in the year, in the calendar format. For example:

cal -j apr 2010

This produces:

        April 2010
 Su  Mo  Tu  We  Th  Fr  Sa
                 91  92  93
 94  95  96  97  98  99 100
101 102 103 104 105 106 107
108 109 110 111 112 113 114
115 116 117 118 119 120

You can easily tell how many days have ran in April during 2010. The -j options can be appended to any date. This can be quite confusing to which actual day represents which number in the calendar. As a result I run this code to show both dates at the same time.

        April 2010
 Su  Mo  Tu  We  Th  Fr  Sa
                 91  92  93
 94  95  96  97  98  99 100
101 102 103 104 105 106 107
108 109 110 111 112 113 114
115 116 117 118 119 120

     April 2010
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

Here it is clear that April the 14th is the 104th day in the year. In the next post I will show you how you can pick the day number out more easily.

The final option is to show how many weeks have passed in the year. This can only be done with ncal and is used with the -w option.

ncal -w

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

Note the week number at the bottom of the calendar. This option can be combined with the day number option and used for any month within any year.

Overall this cal options is useful. You can produce a whole variety of calendars in a graphical format. Tomorrow I will show you the date function for showing date and times and using the option to highlight specific dates. If you want to learn more about Terminal, either look through this category on my site, search my site, or have a look at the many books on Amazon.


If you want to keep up with the latests post from Mac Tricks And Tips I recommend you subscribe to the RSS Feed.

Where To Next?

  • Subscribe To Mac Tricks And Tips