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
06/23/2003

$ datemath '12/25/2003 - today'
188

$ datemath today + 5 weeks
07/25/2003

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
10/24/2003

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.

TODAY
a synonym for today's date
YESTERDAY
a synonym for TODAY-1
TOMORROW
a synonym for TODAY+1
FDAY(date)
Returns the date of the first day of the month 1..31
LDAY(date)
Returns the date of the last day of the month 1..31
DAY(date)
Return the day of the month 1..31
MONTH(date)
Returns the number of month 1..12
YEAR(date)
Returns the year in four-digit form
NDAYS(date)
Returns the number of days in the given month 1..31
n WEEKS
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.

-V
This displays the program's version information, and exits.
-4
Display dates with four-digit years (the default)
-2
Display dates with two-digit years
-C
(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.

Bugs

Download

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
datemath.c
gram.y
lex.l
util.c
jdate.c
defs.h
Makefile

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