Example

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
    1335

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
    24

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
    29:42
    print difference now 10-aug-2003
    474:13:18
    print difference now modified? %rebol.exe
    570:05:19
    print difference now 1-jan-2000
    32082:21:41

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
    18:19:51
    print difference now 29-aug-2003/0:00-7:00
    11:19:34

2006 REBOL Technologies REBOL.com REBOL.net