I have a function that will show my data as mmddyy, but I need to know how to add 15 days to that. This is for a script that I wrote that will remind me to pay a bill from the date the email is sent to me. theDate is the date of the email.
Here’s the script as I have it:
on new_date(theDate)
(* need to add 15 days *)
set cDate to theDate
set shortDate to short date string of cDate
set theMonth to word 1 of shortDate
if length of theMonth is 1 then set theMonth to "0" & theMonth
set theDay to word 2 of shortDate
if length of theDay is 1 then set theDay to "0" & theDay
set theYear to (year of cDate) - 2000
set YYear to "0" & theYear
set DateString to theMonth & theDay & YYear
end new_date
to |day-shift date| from d by n to f
tell d + n * days to (1000000 + year mod 100 * (10 ^ ((3 - (my (offset of "y" in f))) * 2)) + (its month) * (10 ^ ((3 - (my (offset of "m" in f))) * 2)) + day * (10 ^ ((3 - (my (offset of "d" in f))) * 2)) as text)'s text 2 thru -1
end |day-shift date|
|day-shift date| from (current date) by -90 to "dmy" --> "110506"
|day-shift date| from (current date) by 60 to "dmy" --> "081006"
|day-shift date| from (current date) by 90 to "ymd" --> "061107"
(* or even - going back to the original question... *)
|day-shift date| from (current date) by 15 to "mdy" --> "082406"
Neat :lol: ; I wasn’t able to figure out how to shift them around that way (I had several lines of script).
I also failed to find a way to make the shift unit a variable:
to |Shift a Date| from givenDate thru shiftAmount by Units into Format
return {givenDate, shiftAmount, Units, Format}
end |Shift a Date|
set d to |Shift a Date| from (current date) thru 15 by "Months" into "ymd"
The problems are that months isn’t like days, you can’t pass units like days as arguments, and OS X dates don’t handle week numbers.
Obviously adding 5 months to a date is just ‘set month of now to 5 + (month of now)’, but that doesn’t work for weeks.
I suppose you could do something like this, which accepts the constants days and weeks - or otherwise takes some kind of string:
to |shift date| from d for n by u to f
if u starts with "y" then
set d's year to n + (d's year)
else if u starts with "m" then
set d's month to n + (d's month)
else
if u starts with "w" then
set u to weeks
else if u starts with "d" then
set u to days
end if
set d to d + n * u
end if
set y to 10 ^ ((3 - (offset of "y" in f)) * 2)
set m to 10 ^ ((3 - (offset of "m" in f)) * 2)
tell d to (1000000 + year mod 100 * y + (its month) * m + day * (10101 - y - m) as text)'s text 2 thru -1
end |shift date|
|shift date| from (current date) for 2 by days to "dmy" (* units: days/"days"/"d", weeks/"weeks"/"w", "months"/"m", "years"/"y" *)