Kevin
Kevin

Reputation: 1106

AppleScript (or swift) add hours to time

I'm trying to add 8Hours to a date (from the clipboard)

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

But this is not working as expected, I'm having the error Can’t make "06/22/2015 08:15:27 " into type number.

Upvotes: 0

Views: 1175

Answers (2)

Tim Bodeit
Tim Bodeit

Reputation: 9693

In Swift, you can call dateByAddingTimeInterval on an NSDate object.
The time interval is measured in seconds.

yourDate.dateByAddingTimeInterval(8 * 60 * 60)

If you wanted to add another method to add 8 hours directly, you could define an extension:

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

Upvotes: 1

Nick Groeneveld
Nick Groeneveld

Reputation: 903

You need to pick the hours as an individual variable, like shown below:

set currentDate to current date

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

You can also use this for days, minutes and seconds.

This will work. You can then use the variables to construct a new date to be used in the display dialog.

PS. Don't forget to change the day if the newHour variable is bigger than 24 hours.

EDIT

Setting a date to the clipboard can be done like this:

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

Getting the current clipboard and adding it to a variable goes like this:

set currentDate to get the clipboard
display dialog currentDate

I hope this helps!

Upvotes: 2

Related Questions