Converting an ISO 8601 timestamp into an NSDate: How does one deal with the UTC time offset?

No need to remove the :’s. To handle the “00:00” style timezone, you just need “ZZZZ”: Swift let dateString = “2014-07-06T07:59:00Z” let dateFormatter = NSDateFormatter() dateFormatter.locale = NSLocale(localeIdentifier: “en_US_POSIX”) dateFormatter.dateFormat = “yyyy-MM-dd’T’HH:mm:ssZZZZ” dateFormatter.dateFromString(dateString) Objective-C NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; dateFormat.locale = [[NSLocale alloc] initWithLocaleIdentifier:@”en_US_POSIX”]; NSString *input = @”2013-05-08T19:03:53+00:00″; [dateFormat setDateFormat:@”yyyy-MM-dd’T’HH:mm:ssZZZZ”]; //iso 8601 format NSDate … Read more

Swift 3 – find number of calendar days between two dates

In Swift 5 there is a simple one-liner to get the number of days (or any other DateComponent) between two dates: let diffInDays = Calendar.current.dateComponents([.day], from: dateA, to: dateB).day Note: As pointed out in the comments, this solution measures the 24h periods and therefore requires at least 24h between dateA and dateB.

How to get time (hour, minute, second) in Swift 3 using NSDate?

In Swift 3.0 Apple removed ‘NS’ prefix and made everything simple. Below is the way to get hour, minute and second from ‘Date’ class (NSDate alternate) let date = Date() let calendar = Calendar.current let hour = calendar.component(.hour, from: date) let minutes = calendar.component(.minute, from: date) let seconds = calendar.component(.second, from: date) print(“hours = \(hour):\(minutes):\(seconds)”) … Read more