applescript - 特定月份有多少天(指定日期)
applescript - How many days in a specific month (specified date)
概览
- 我正在尝试提供指定日期并找出该月有多少天。
- 找到了接近的东西,但它使用的是当前日期。我如何修改当前日期以便它在下面的子程序输入中接受它
愿望:
- 获取从指定日期开始一个月中的天数
- 输入(MM/DD/YYYY)得到天数
样本:
on GetNumberOfDaysInMonth(aDate)
local aDateCopy
copy aDate to aDateCopy
set day of aDateCopy to 1
if month of aDateCopy is December then
set year of aDateCopy to ((year of aDateCopy) + 1)
set month of aDateCopy to 1
else
set month of aDateCopy to ((month of aDateCopy) + 1)
end if
return day of (aDateCopy - 1 * days)
end GetNumberOfDaysInMonth
my GetNumberOfDaysInMonth(current date)
没关系我想出来了。
set newDate to current date
set the month of newDate to (2 as integer)
set the day of newDate to (1 as integer)
set the year of newDate to (2021 as integer)
my GetNumberOfDaysInMonth(newDate)
在这种情况下,存储一个可以在需要时查找的已知数量列表总是更有效,而不是调用处理程序(速度较慢)然后让该处理程序执行一堆操作(这仍然更慢):
set numberOfDaysInMonth to {31, 28 + leap, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
其中 leap
由表达式给出,y
:
(y mod 4 = 0 and y mod 100 ≠ 0 or y mod 400 = 0) as integer
或者,同样徒劳无功,可以改为存储为预定义列表。
也就是说,下面是您的处理程序的 corrected/refined 版本。由于某些原因,您最好不要以现有方式处理在 12 月到 1 月过渡等情况下出现的边缘情况。 AppleScript date
对象设计为通过增加或减少秒数来调整。调整其任何其他属性(day
、month
或 year
)需要考虑此类更改可能产生的连锁反应。
on numberOfDaysInMonth(input as {date, integer})
local input
if the input's class ≠ date then tell the (current date) ¬
to set [input, day, its month] to [it, 1, input]
tell the input to set [day, its month] to [1, 1 + (its month)]
return the day of (input - days)
end numberOfDaysInMonth
这接受 AppleScript date
class 对象(例如 current date
返回的对象,或者只是一个由其数值(1–12,可以是 integer
或 string
/text
class 对象)或作为 AppleScript month
class 对象(January
、February
、March
等——请注意,这些是 而不是 字符串值) . 将月份传递给处理程序的地方,它 returns 当前年份的月份将(已经)拥有的天数。一些示例:
get the numberOfDaysInMonth(current date)
--> 31
get the numberOfDaysInMonth(December)
--> 31
get the numberOfDaysInMonth(2)
--> 28
概览
- 我正在尝试提供指定日期并找出该月有多少天。
- 找到了接近的东西,但它使用的是当前日期。我如何修改当前日期以便它在下面的子程序输入中接受它
愿望:
- 获取从指定日期开始一个月中的天数
- 输入(MM/DD/YYYY)得到天数
样本:
on GetNumberOfDaysInMonth(aDate)
local aDateCopy
copy aDate to aDateCopy
set day of aDateCopy to 1
if month of aDateCopy is December then
set year of aDateCopy to ((year of aDateCopy) + 1)
set month of aDateCopy to 1
else
set month of aDateCopy to ((month of aDateCopy) + 1)
end if
return day of (aDateCopy - 1 * days)
end GetNumberOfDaysInMonth
my GetNumberOfDaysInMonth(current date)
没关系我想出来了。
set newDate to current date
set the month of newDate to (2 as integer)
set the day of newDate to (1 as integer)
set the year of newDate to (2021 as integer)
my GetNumberOfDaysInMonth(newDate)
在这种情况下,存储一个可以在需要时查找的已知数量列表总是更有效,而不是调用处理程序(速度较慢)然后让该处理程序执行一堆操作(这仍然更慢):
set numberOfDaysInMonth to {31, 28 + leap, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
其中 leap
由表达式给出,y
:
(y mod 4 = 0 and y mod 100 ≠ 0 or y mod 400 = 0) as integer
或者,同样徒劳无功,可以改为存储为预定义列表。
也就是说,下面是您的处理程序的 corrected/refined 版本。由于某些原因,您最好不要以现有方式处理在 12 月到 1 月过渡等情况下出现的边缘情况。 AppleScript date
对象设计为通过增加或减少秒数来调整。调整其任何其他属性(day
、month
或 year
)需要考虑此类更改可能产生的连锁反应。
on numberOfDaysInMonth(input as {date, integer})
local input
if the input's class ≠ date then tell the (current date) ¬
to set [input, day, its month] to [it, 1, input]
tell the input to set [day, its month] to [1, 1 + (its month)]
return the day of (input - days)
end numberOfDaysInMonth
这接受 AppleScript date
class 对象(例如 current date
返回的对象,或者只是一个由其数值(1–12,可以是 integer
或 string
/text
class 对象)或作为 AppleScript month
class 对象(January
、February
、March
等——请注意,这些是 而不是 字符串值) . 将月份传递给处理程序的地方,它 returns 当前年份的月份将(已经)拥有的天数。一些示例:
get the numberOfDaysInMonth(current date)
--> 31
get the numberOfDaysInMonth(December)
--> 31
get the numberOfDaysInMonth(2)
--> 28