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

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