How to add length of time to a date?

I am using applescript to take dates from excel spreadsheets, and depending on the category, I need it to either add the date as is in ical as a todo, and in other cases i need to add 1 week, or 2 weeks, to the time frame.

I got 1 week to work by adding:


if item i of summaryList is "ZZZ" then set theDate to theDate + weeks

But how do I add 2 weeks?
And what if i want to add a specific amount of days? i.e. 3 days

nevermind…found it after refining my search terms at this site and in google :stuck_out_tongue:
for those interested:

2 weeks later:

if item i of summaryList is "ZZZ" then set theDate to theDate + weeks * 2

3 days later:

if item i of summaryList is "ZZZ" then set theDate to theDate + days * 3

Hi, bluenote.

I believe (but don’t know for sure) that the reason AppleScript’s time constants have plural names is to allow vaguely English-looking expressions when numbers are multiplied by them:

theDate + 2 * weeks
theDate + 3 * days

These work fine:

(current date) + 10 * weeks
(current date) + 10 * days
(current date) + 10 * hours
(current date) + 10 * minutes

But these don’t:

(current date) + 10 * years -- (Error: "The variable years is not defined.")
(current date) + 10 * year -- (Error: "Can't make year of <script> into type number.")
(current date) + 10 * months -- (Error: "Can't make every month of <script> into type number.")
(current date) + 10 * seconds -- (Error: "Can't make seconds into type number.")

What’s up with these? Is there similarly a syntactically elegant way of adding years, months and seconds, without resorting to convoluted calculations? (Which I know how to do - just wondering.)

weeks have a constant amount of seconds, years and months have not.

adding seconds is very easy

(current date) + 10

To add a year you can use this


set currentDatePlusOneYear to (current date)
set year of currentDatePlusOneYear to (get (year of currentDatePlusOneYear) + 1)
currentDatePlusOneYear

Ah! Of course! AppleScript bases everything on seconds. This is the crux of why AppleScript cannot directly so work with years & months; one therefore must resort to the kind of what I called “convoluted calculations” as Stefan described. Thanks, Stefan!

That this type of calculation works depends on the the fortunate property of AppleScript to do what might be called “modulo overflow” when the values exceed the limits of, e.g., months in a year: when you add enough months, it’s smart enough to overflow into incrementing the year, too.

But I’ve noticed that when you add too large a number past a certain point (for which I haven’t discerned rhyme or reason yet), it doesn’t overflow correctly:
For example, with:
current date = date “Wednesday, November 4, 2009 11:35:18 AM”:

set newDate to (current date)
set month of newDate to (month of newDate) + 4
newDate

newDate = date “Wednesday, November 4, 2009 11:37:41 AM”

…but incrementing one more month:

set newDate to (current date)
set month of newDate to (month of newDate) + 5
newDate

…yields the unwanted newDate = date “Friday, September 27, 2030 11:41:16 AM”

How come the sudden large jump in the year?

Look at this article, there are described several methods to calculate dates and times in AppleScript

Yes, thanks - very useful article. Actually I had found it earlier, and it acknowledges the excessive overflow errors I mentioned, and thankfully comes up with some algorithms to workaround them. I guess I was just wondering whether anyone knows why AppleScript fails so abruptly after directly adding only so few months out? Is it a bug (or a feature)?

Or a “fug”?
Definition - FUG: noun, a F_eature that would be called a b_UG, had not the usability-clueless programmers deliberately included it.

Common usage - “To Microsoft”: “To FUG-up!”

It’s not a bug - it’s an undocumented feature.

If it happens once, it’s a bug. If it happens twice, it’s a feature. If it happens more than twice, it’s a design philosophy.

One bug is just another unsupported feature.

Heh-heh!

Or is it another one of these atrocities we learned about only yesterday?:
Snow Leopard: Don’t get caught in date handling change in AppleScript, by David Morgenstern
http://blogs.zdnet.com/Apple/?p=5148&tag=post-5148;interact_Apple_5148