Simple Command Line Date/Time Calculations

Being a contractor I have to sent my customers an invoice every month. With regards to payment terms I generally settle on a pretty standard 30 day credit period. However sometimes the customer and I settle on a slightly different credit period. Regardless of the credit period I always have to think carefully what the final due date will be. It’s nothing complicated, the calculation being a simple modulo operation, but I always have to determine the number of days in a given month in order to use the correct divisor.

Eg. given today’s date, October 8th, 2008, a credit date of 45 days results in a due date of:

(8 + 45) % 31 = 22

November 22nd, 2008. The 31 being the number of days in October.

However I recently found out that the date command under OS X can do this even more easily:

$ date -v +45d
Sat Nov 22 21:11:56 CET 2008

The parameter -v is the adjust parameter. It not only handles days as in the example above, but all the other units of time as well. Simple and convenient.

Comments !

social