I run this without problem in playground. And no optional
I had to do some changes:
- delete *Int(timestamps) : what is this ? May be that's the cause of optional, ;
try *Int(timestamps)! if yoy are sure it's convertible to Int
- delete the unwrap for dateStarted in
let dateStartedLocal = dateFormatter.stringFromDate(dateStarted!)
NSDate (and Swift 3’s
Date) does not support the concept of time zones. Under the covers an NSDate is actually a floating point count of the number of seconds since the reference date (the start of 2001). NSDate’s
-descriptionmethod always returns a UTC string to reinforce that fact.
Generally you should use NSDate to hold date values and then use NSDateFormatter when you want to ‘render’ those values to something the user can read (or parse values the user has entered). However, there are circumstances where it makes sense to store other things (like NSDateComponents).
In your specific case, AFAICT, you just want to calculate an NSDate from an offset in minutes relative to the current date. You don’t need to involve calendar to do this. Rather, you can do it with simple maths:
import Foundation let deltaMinutes: NSTimeInterval = -35.0 let now = NSDate() let then = NSDate(timeIntervalSinceNow: deltaMinutes * 60.0) print(now) print(then)
For example, running this produces:
2016-10-20 08:43:34 +0000 2016-10-20 08:08:34 +0000
This is safe to do for minutes (and any smaller units) because small time units are the same in all calendars. If this were hours then things get tricky, because you have to decide what ‘N hours from now’ means when crossing a time discontinuity (like a daylight savings transition).
Date and time programming is really hard to get right in the general case. I recommend you watch WWDC 2011 Session 117 Performing Calendar Calculations, which describes some of the really gnarly edge cases.
Also, if you have specific questions feel free to post them here. Try to describe what you’re high-level goạl is, rather than getting bogged down in the code, because we need that context in order to give the best advice.
Share and Enjoy
Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware
let myEmail = "eskimo" + "1" + "@apple.com"