所以我试图定义一个增加数月的函数。但是从一开始,我就偶然遇到以下问题。
set testdate to (current date)
set the month of testdate to (1 as integer)
set the day of testdate to (1 as integer)
tell testdate to set {day} to {day + 32 * 4}
testdate
我希望至少得到一个日期为2020年5月的输出(当然不是5月1日,而是开头的东西,但是输出是
date "Montag, 26. August 2019 um 15:53:13"
相反。有人可以向我解释吗?
例如,如果将上面的“ 4”更改为“ 3”,则可以得到预期的结果
date "Montag, 6. April 2020 um 16:06:21"
借助于red_menace,我能够执行以下过程,这增加了数月的时间。
on addMonths onto oldDate by m
copy oldDate to newDate
set newDate to newDate + (days * 31 * m)
if newDate's day is not oldDate's day then set newDate to (newDate) - ((newDate's day) - (oldDate's day)) * days
return newDate
end addMonths
(但是,仍然没有关于此错误发生原因的解释。)
我不认为这是一个错误,它可能只是内部日期对象实现方式的结果(我的猜测是127个有效,而128个无效)。 day
属性适用于当月的某天(1-31),因此使用越界天数作弊可能会导致几天充斥到附近的月份中,但他们可能不会费心检查是否超出分配结构的大小。
处理日期时,通常的时差程序是增加或减少秒数。日期类具有各种属性,例如day
和month
,但也有诸如days
或hours
之类的常数,它们分别包含一天或一小时中的秒数(它们仅转到[ C0],因为月份没有固定的天数):
weeks
请参阅AppleScript语言指南中的set testdate to (current date) -- get a date object
set the month of testdate to 1 -- set new month
set the day of testdate to 1 -- set new day
log 24 * hours
log days
return testdate + (days * 32 * 4) -- add to the date
。