Previous Book Contents Book Index Next

Inside Macintosh: AppleScript Language Guide / Part 2 - AppleScript Language Reference
Chapter 6 - Expressions / Operations

Date-Time Arithmetic

AppleScript supports these operations with the + and - operators on date and time difference values:

date + timeDifference
--result: date
date - date
--result: timeDifference 
date - timeDifference
--result: date
where date is a date value and timeDifference is an integer value specifying a time difference in seconds.

To simplify the notation of time differences, you can also use one or more of these constants:
hours60 * minutes
days24 * hours
weeks7 * days

Here's an example:

date "Apr 15, 1992" + 4 * days + 3 * hours + 2 * minutes
It is often useful to be able to specify a time difference between two dates;
for example:

set timeInvestment to current date - "May 16, 1992"
After running this script, the value of the timeInvestment variable is an integer that specifies the number of seconds between the two dates. If you then add this time difference to the starting date (May 16, 1992), AppleScript returns a date value equal to the current date when the timeInvestment variable was set.

To express a time difference in more convenient form, divide the number of seconds by the appropriate constant:

31449600 / years
--result: 1
151200 / days 
--result: 1.75
To get an integral number of hours, days, and so on, use the div operator:

151200 div days 
--result: 1
To get the difference, in seconds, between the current time and Greenwich mean time, use the scripting addition command Time to GMT. For example, if you are in Cupertino, California, and your computer is set to Pacific Standard Time, Time to GMT produces this result:

time to GMT
--result: -28800
For more information about the Time to GMT command, see the AppleScript Scripting Additions Guide.

Previous Book Contents Book Index Next

© Apple Computer, Inc.
13 JUL 1996