使用 applescript 获取任意日期的工作日
Get weekday of arbitrary date with applescript
我正在编写脚本来创建 table 内容。此目录中每一项的格式为:工作日、月日。示例:9 月 1 日,星期日。我有一个格式为“09/01/2018”的日期。我可以使用这个日期来获取工作日吗?当我尝试以下操作时,出现错误 "BBEdit got an error: Can’t get weekday of date "09/01/2018".""
set theDate to theMonth & "/" & theDay & "/" & theYear --> "09/01/2018"
set theWeekday to (weekday of date theDate)
我还尝试将日期 "as date" 设置为 "Can’t make "09/01/2018" 类型日期。"我也尝试添加 "tell application "{BBEdit, Finder, System Events}" to set theDate..." 并且每个都给出与上面相同的错误。
谷歌搜索时我似乎能找到的唯一信息是如何根据 "today" 获取 currentDate 或其他日期。只是一个随机日期,这就是我需要的。
我确定我只是遗漏了一些非常简单的东西,但我是 AppleScript 的新手,所以非常感谢任何帮助!
提前致谢!
最可靠(和语言环境无关)的方法是使用 current date
创建日期并设置属性
set theMonth to 8
set theDay to 14
set theYear to 2018
set currentDate to current date
-- the first `day` avoids an overflow error
tell currentDate to set {day, year, its month, day} to {1, theYear, theMonth, theDay}
set theWeekday to weekday of currentDate
或在 Cocoa
的帮助下
use framework "Foundation"
set dateComponents to current application's NSDateComponents's alloc()'s init()
dateComponents's setYear:theYear
dateComponents's setMonth:theMonth
dateComponents's setDay:theDay
set theDate to (current application's NSCalendar's currentCalendar()'s dateFromComponents:dateComponents) as date
set theWeekday to weekday of theDate
我正在编写脚本来创建 table 内容。此目录中每一项的格式为:工作日、月日。示例:9 月 1 日,星期日。我有一个格式为“09/01/2018”的日期。我可以使用这个日期来获取工作日吗?当我尝试以下操作时,出现错误 "BBEdit got an error: Can’t get weekday of date "09/01/2018".""
set theDate to theMonth & "/" & theDay & "/" & theYear --> "09/01/2018"
set theWeekday to (weekday of date theDate)
我还尝试将日期 "as date" 设置为 "Can’t make "09/01/2018" 类型日期。"我也尝试添加 "tell application "{BBEdit, Finder, System Events}" to set theDate..." 并且每个都给出与上面相同的错误。
谷歌搜索时我似乎能找到的唯一信息是如何根据 "today" 获取 currentDate 或其他日期。只是一个随机日期,这就是我需要的。
我确定我只是遗漏了一些非常简单的东西,但我是 AppleScript 的新手,所以非常感谢任何帮助!
提前致谢!
最可靠(和语言环境无关)的方法是使用 current date
创建日期并设置属性
set theMonth to 8
set theDay to 14
set theYear to 2018
set currentDate to current date
-- the first `day` avoids an overflow error
tell currentDate to set {day, year, its month, day} to {1, theYear, theMonth, theDay}
set theWeekday to weekday of currentDate
或在 Cocoa
的帮助下use framework "Foundation"
set dateComponents to current application's NSDateComponents's alloc()'s init()
dateComponents's setYear:theYear
dateComponents's setMonth:theMonth
dateComponents's setDay:theDay
set theDate to (current application's NSCalendar's currentCalendar()'s dateFromComponents:dateComponents) as date
set theWeekday to weekday of theDate