But there are lots of options. As our friend FHQWHGADS said, chapter 13 in the SenseTalk manual will give you more information than you ever wanted to know on the subject…
How does one ensure that the returned value is, in the case where the month value is < 10, a two digit value?
The input fields that I have want to see DDMMYYYY values.
Using
put the long date into ThisDate
put (Day of TheDate) & "/" & ( Month of TheDate) & "/" & ( year of TheDate) into ThisDate
repeat with ThisItem = each Item Delimited By "/" of ThisDate
typetext ThisItem
end repeat
Gives me “7” in the month, rather than “07”, and then I end up with a value along the lines of “14/72/008_”
Not pretty.
On a related note
put the long date + 1 year into anotherdate
Doesn’t work, although my reading of the reference manual suggests that it should.
put the long date + 52 weeks into anotherdate
almost works, but comes up one day short. (leap year perhaps?)
The day, month, and year functions each return a number (not a formatted string) so using them won’t give you leading zeros for single-digit days or months. It could be done by setting the numberFormat, but for what you want there’s an easier way, using the formattedTime() function. This should do what you want:
TypeText formattedTime("%d%m%Y", TheDate)
The formatting codes that you can use with this function are described in the documentation for the timeFormat global property.
Regarding your other question (adding a year to a date), SenseTalk’s predefined time interval values only go up through weeks, and don’t currently include months and years since they aren’t a consistent number of seconds long. To get a date that’s a year in the future, you can add 365 days, except of course that won’t be correct for leap years.
So to correctly add a calendar year, the best approach is probably to convert to the dateItems format, which separates the date into separate values for year, month, day, and so forth. In this format you can add or subtract any number of years, months, days, etc. by dealing with the appropriate item. Then convert back to the final format that you want, like this:
set myDate to today
convert myDate to dateItems
add 1 to item 1 of myDate -- item 1 is the year
convert myDate to short date -- or whatever format you want
put myDate
The dateItems formats are often overlooked, but can be quite useful for doing this sort of calculation.