This is an example of how to take the information provided by “current date” and bend it to your will. By Dan Decker, with some help from the MacScripter BBS
OS version: Any
property theMonths : {January, February, March, April, May, June, July, August, September, October, November, December}
set theDate to current date
set theDay to the day of theDate
set theYear to the year of theDate
set theMonth to the month of theDate
if theDay < 10 then
set dayZero to 0 as string
else
set dayZero to "" as text
end if
repeat with x from 1 to 12
if theMonth = (item x of theMonths) then
set theMonth to x
exit repeat
end if
end repeat
if theMonth < 10 then
set theZero to 0 as string
else
set theZero to "" as text
end if
Things have become a little bit easier since the original post: you no longer have to loop through the months in order to get their ordinal value.
I thought I could add some more to dates while I were still at it.
I want to show a clever technique I first saw in a post by Julio to create a number string with leading zero’s. Then I might as well show a method of setting the date which works wherever we are.
Then how to find the difference in days between to dates, which should work everywhere. -Even places with terminology clashes, (which means that two different dictionaries has their own definition of a word at the same time, - making the word unusable).
Everything here should work from Tiger and onwards ( it is really only the coercing of a month to integer, which makes it stop going further backwards than Tiger).
Agenda Universal function for setting date
Coerce month to integer
Create a date string
Calculate number of days between two dates
set theDate to setDate(2009, 7, 11, (current date))
set theDay to the day of theDate
set theYear to the year of theDate
set theMonth to the month of theDate as integer
set theMonth to text -2 thru -1 of ("0" & theMonth)
set theDay to text -2 thru -1 of ("0" & theDay) -- from Julio
set {astid, text item delimiters} to {text item delimiters, "."}
set datestr to {"" & theYear, theMonth, theDay} as text -- "2009.07.11"
set text item delimiters to astid
-- find the difference between two dates:
set theDays to ((current date) - theDate) / (3600 * 24) as integer -- 354
-- The the absence of the day constant is deliberate, since it won't always work from within an applications tell block
log theDays
on setDate(intYear, intMonth, intDay, dateObject)
set day of dateObject to 1 ” thanks to Shane Stanley!
set year of dateObject to intYear
set month of dateObject to intMonth
set day of dateObject to intDay
return dateObject
end setDate
Edit:
I managed of course to not taking the overflow into account, see Shane’s handler below for the perfect one.
I updated this one, just in case, you should use it for interest rate or something.
Best regards
That handler is not reliable. For example, try calling it today (June 30 here) like this:
set theDate to setDate(2009, 2, 11, (current date))
You need to modify it so that it first either sets the day to 28 or less, or sets the month to one that contains 31 days. For example:
on setDate(intYear, intMonth, intDay, dateObject)
set day of dateObject to 1
set year of dateObject to intYear
set month of dateObject to intMonth
set day of dateObject to intDay
return dateObject
end setDate