I'm trying to figure out how to get a readable time amount from timeIntervalSinceDate. Right now I have an NSDate with key "punchInTime" stored in NSUserDefaults, but I don't know how to get a readable time back when I want to find the difference between the stored NSDate "punchInTime" and the current time. I have:
var totalWorkTime = NSDate.timeIntervalSinceDate(punchInTime)
I'd like to interpolate "totalWorkTime" into a string to have readable time amounts.
Any help appreciated! Thanks.
Fix your syntax. It should be:
var totalWorkTime = NSDate().timeIntervalSinceDate(punchInTime)
Note the parenthesis after NSDate
. If you do not use parenthesis, and you try to treat totalWorkTime
as an NSTimeInterval you will get the error Cannot invoke [operator] with an argument list of type ...
You can get a better error message if you specify the type in the variable assignment. The error is (x) -> y is not convertible to y
Swift is a functional language. Without the parenthesis after NSDate
then totalWorkTime
is not a symbol for a mere NSTimeInterval type, but rather it is a symbol for a function that takes an NSDate and returns a NSTimeInterval. This syntax in a playground shows:
Other than what I wrote above, zisoft is correct about this question (almost) being a duplicate. The other minor exception is that this question is asking about a Double
(NSTimeInterval
) type and the earlier question is asking about Int
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With