Date calculations with stock and Gnu versions of 'date'

Jul 25, '08 07:30:04AM

Contributed by: rahulbenegal

The stock OS X date command ships with a useful -v command that allows date calculation. For example, to determine the last day of February, you could use the following:

date -v3m -v1d
That's the third month in the first -v, and then less one day in the second -v. man date gives many examples. On the other hand, the Gnu version of date that's available from MacPorts does not support this option. Playing a bit with the --date option for Gnu's date, I came up with:
date --date="march 1 1 day ago"
This results in the same date calculation as in the stock date function. Gnu's date also includes the option of printing rfc-2822 and rfc-3339 dates. The latter can be used for applications like Google that require xls dates:
date --rfc-3339='ns'
2008-07-23 18:28:00.110568000+05:30
The ns means nano-seconds.

Given the different features of the two programs, it might be useful to keep both on your system, with the Gnu version of date being stored as gdate.

Comments (6)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20080723060344662