Does this site look plain?

This site uses advanced css techniques

Too many times we have needed to do a bit of math on a date -- say, "today + 7 days" -- but in traditional MM/DD/YYYY format this is really tricky (especially in a shell script). A result of this need we built the datemath tool which can perform these functions from the command line or from a shell script. Examples:

$ datemath today + 5

$ datemath '12/25/2003 - today'

$ datemath today + 5 weeks

when will my machine be up for one year?
$ uptime
 11:09am  up 317 days, 15:38,  7 users,  load average: 0.16, 0.04, 0.01
$ datemath today + 365 - 317

We've been using this for 15 years, and recently got around to porting it and updating for year-2000 compatibility (oops). The port to Bison/flex from yacc/lex was a bit of doing as well.

Date expressions accepted

The expressions accepted by datemath are straightforward and moderately flexible. The grammer is defined to reject most things that don't make any sense (such as adding two dates together), while allowing most things that do.

Dates can be entered in one of three formats:

Keywords are shown here in upper case to make them stand out, but the program doesn't care at all about case, and mixed upper/lower work fine. Remember that parentheses are special to the shell, so they must be quoted.

a synonym for today's date
a synonym for TODAY-1
a synonym for TODAY+1
Returns the date of the first day of the month 1..31
Returns the date of the last day of the month 1..31
Return the day of the month 1..31
Returns the number of month 1..12
Returns the year in four-digit form
Returns the number of days in the given month 1..31
Returns the number of days in n weeks
date + ndays
Return the date ndays into the future
date - ndays
Return the date ndays into the past
date - date
Return the number of days between the two dates

These can be combined in most combinations that make sense, though we've never gotten too elaborate about making sure that it's completely orthogonal.

Command Line Parameters

In addition to the date expression above (required), a few command-line parameters are supported.

This displays the program's version information, and exits.
Display dates with four-digit years (the default)
Display dates with two-digit years
(Collation mode) Display dates in year/month/day format that is useful for sorting.
-9 ##
When interpreting a date with a two-digit year, years from ##..99 are 1900, otherwise 2000. Default value = 40.



The source is delivered as a gzip'd GNU tar archive (do a "save as"):

datemath.tar.gz - compressed tarball of source

The files inside are:

$ gtar -tzf datemath.tar.gz

Note that this package requires GNU flex and bison: those needing to use the older tools may have some troubles. Reports of portability issues are gratefully accepted.

Version History

Fri Jun 20 23:01:46 PDT 2003 -- version 2.0.10
-- initial release to website