Working With The Date Function In Terminal 2

Terminal

Share
Working With The Date Function In Terminal

Hey

Yesterday I wrote a post on the calendar function in Terminal, its designed as a two part series, with the second part being about the date function. I found in bash scripts the two functions sort of work hand in hand. This post is going to show you some of the main features of the date function. The date function is more versatile in that you can easily pick out the piece of information you want, the week of the year, day of the year etc, the calendar function is more graphical. The date function can be used to set the date in your Mac, however I will not be going into this today as I prefer to use System Preferences (to find out more about setting the date use the man help page).

Before we start I would like to point out a couple of cool books on Amazon which may be of use if you want to pursue this type of programming. My favourite is Mac OS X Unix Toolbox although there is a variety on Amazon.

The date function is accessed in Terminal simply by typing date this will display something similar to the following.

Sun 31 Jan 2010 20:58:03 GMT

That’s useful on its own. However if you are using a bash script any want something specific the default format displayed by ‘date’ is a bit basic. As a result we are going to use some formatting options to pick apart the date function and allow use to display what we want. This is done using the following:

date '+%A'

The previous code will display the long hand day of the week, eg “Sunday”. What is important to note is the %A, these can be a variety of options which I will discuss in a minute. Anything that doesn’t have an ampersand before the letter will be displayed by text, the single quote marks and the plus sign need to be added otherwise the script will result in an error, any other text added will be displayed in full.

There are a variety of format options which can be used. A more comprehensive list can be found here, the best ones are below.

%a = Short date, Sun
%A = Long date, Sunday
%b = Short Month, Feb
%B = Long Month, February
%d = Day of the month
%D = Date such as %m/%d/%y
%H = Hour in 24 hour format (00..23)
%I = Hour in 12 hour format (01..12) (this can be used with %p to append Am or PM)
%j = Day of the year
%m = Month in number format (01..12)
%M = Minute (00..59)
%p = Locale either AM or PM
%S = Second
%u = Day of the week
%V = Week number of year with Monday as first day of week (01..53)
%Y = Year
%z = Numeric timezone (e.g., -0400) or %Z with timezone abbreviation.

You can also use %t, for a tab and %n for a new line. Combining these together you can produce something quite useful. The following:

date '+The Current Time:%nYear: %Y%nMonth: %m%nWeek: %V%nDay: %A (%d of the month, %j of the year)%nHour: %H%nMinute: %M%nSecond: %S'

Will produce:

The Current Time:
Year: 2010
Month: 01
Week: 04
Day: Sunday (31 of the month, 031 of the year)
Hour: 21
Minute: 13
Second: 59

Very interesting if you want to use it in GeekTool or a script. You can mix and match any of the options to produce what you want. Note that the more options you use the long it can take to work out. I have a feeling your Mac works everything out from the initial date (the time is stored in seconds so the original t value = 0), so the more options you have the longer it will take to run.

Highlight the Current Date In GeekTool

You can use the date function to run with the cal function to highlight the current date in GeekTool (even in Terminal). I didn’t make this code I found it here. You can play around with the code to produce different results. You can change the colour with the colour codes, these can be found at the bottom of the page here.

cal_head=`cal | head -1`; cal_tail=`cal | tail -7`; today=`date "+%e"`; echo "$cal_head"; echo -en "${cal_tail/${today}/\033[1;32m${today}\033[0m}";

Conclusion

The date function is very versatile can be used to give a lot of information. Its good for displaying information and working out factors based on the current time. If you have any tips or tricks please leave a comment below.


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