Computing Time Differences

Author: Carl Sassenrath
Return to REBOL Cookbook

This example shows how to easily calculate the difference between two dates or times.

To compute the number of days between two dates, just subtract them. To get a positive number, put the more recent date first:

    print 28-aug-2003 - 1-jan-2000

Using this, you can do things like show how many days old you are:

    birthday: 6-jun-1982
    print ["You are" now - birthday "days old today."]
    You are 7754 days old today.

Or even how old a file is:

    print now - modified? %rebol.exe

Sometimes you want to show the difference as a time (hours) not as days. In newer versions of REBOL the DIFFERENCE function will return the time difference between two date-time values:

    print difference 28-aug-2003/11:12 27-aug-2003/5:30
    print difference now 10-aug-2003
    print difference now modified? %rebol.exe
    print difference now 1-jan-2000

Note that too large a time span may overflow the number of hours that can be held by the time datatype.

Also take note that the DIFFERENCE function accounts for timezone variations:

    print difference now 29-aug-2003/0:00
    print difference now 29-aug-2003/0:00-7:00

2006 REBOL Technologies REBOL.com REBOL.net