AppleScript(或 swift)增加时间

AppleScript (or swift) add hours to time

我正在尝试将 8 小时添加到日期(来自剪贴板)

set date1 to the clipboard
set newdate to date1 + (8 * hours)
display dialog "Purchases were downloaded at " & newdate buttons {"OK"} default button 1

但这没有按预期工作,我遇到了错误 Can’t make "06/22/2015 08:15:27 " into type number.

您需要选择小时数作为单个变量,如下所示:

set currentDate to current date

set newHour to ((hours of currentDate) + 8)

您还可以将其用于天数、分钟数和秒数。

这会奏效。然后,您可以使用这些变量构造一个新日期,以在 display dialog.

中使用

PS。如果 newHour 变量大于 24 小时,请不要忘记更改日期。

编辑

可以像这样将日期设置到剪贴板:

set currentDate to current date
set the clipboard to currentDate as Unicode text

获取当前剪贴板并将其添加到变量的过程如下:

set currentDate to get the clipboard
display dialog currentDate

希望对您有所帮助!

在 Swift 中,您可以在 NSDate 对象上调用 dateByAddingTimeInterval
时间间隔以秒为单位。

yourDate.dateByAddingTimeInterval(8 * 60 * 60)

如果你想增加另一种方法直接加8小时,你可以定义一个扩展:

extension NSDate {
    func addEightHours() -> NSDate {
        return self.dateByAddingTimeInterval(8 * 60 * 60)
    }
}