Is there a simple way of converting an ISO8601 timestamp to a formatted NSDate?

I have a similiar but slightly more complex problem, and I’ve found a very simple solution! The problem: My incoming ISO8601 dates look like this: 2006-06-14T11:06:00+02:00 They have a timezone offset at the end. The solution: Use Peter Hosey’s ISO8601DateFormatter which you can download from here. ISO8601DateFormatter *formatter = [[ISO8601DateFormatter alloc] init]; NSDate *theDate = … Read more

Swift – iOS – Dates and times in different format

func convertDateFormater(date: String) -> String { let dateFormatter = NSDateFormatter() dateFormatter.dateFormat = “yyyy-MM-dd’T’HH:mm:ss.SSSZ” dateFormatter.timeZone = NSTimeZone(name: “UTC”) guard let date = dateFormatter.dateFromString(date) else { assert(false, “no date from string”) return “” } dateFormatter.dateFormat = “yyyy MMM EEEE HH:mm” dateFormatter.timeZone = NSTimeZone(name: “UTC”) let timeStamp = dateFormatter.stringFromDate(date) return timeStamp } Edit for Swift 4 func convertDateFormatter(date: … Read more

How to parse a date string into an NSDate object in iOS?

You don’t need near as many single quotes as you have (only needed on non date/time characters), so change this: [self.dateFormatter setDateFormat:@”yyyy’-‘MM’-‘dd’T’HH’:’mm’:’ss’Z'”]; To this: [self.dateFormatter setDateFormat:@”yyyy-MM-dd’T’HH:mm:ssZZZ”]; … self.currentQuestion.updated = [self.dateFormatter dateFromString:[self.currentParsedCharacterData stringByReplacingOccurrencesOfString:@”:” withString:@”” options:0 range:NSMakeRange([self.currentParsedCharacterData length] – 5,5)]]; Documentation here: https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html#//apple_ref/doc/uid/TP40002369-SW1 Unicode Format Patterns: http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns Dealing with TimeZones with Colons (+00:00): http://petersteinberger.com/2010/05/nsdateformatter-and-0000-parsing/

Using DateFormatter on a Unix timestamp

You can convert unixTimestamp to date using Date(timeIntervalSince1970:). let unixTimestamp = 1480134638.0 let date = Date(timeIntervalSince1970: unixTimestamp) If you want to display date in string with specific formate than you can use DateFormatter like this way. let date = Date(timeIntervalSince1970: unixtimeInterval) let dateFormatter = DateFormatter() dateFormatter.timeZone = TimeZone(abbreviation: “GMT”) //Set timezone that you want dateFormatter.locale … Read more

How to convert string to date to string in Swift iOS? [duplicate]

First, you need to convert your string to NSDate with its format. Then, you change the dateFormatter to your simple format and convert it back to a String. Swift 3 let dateString = “Thu, 22 Oct 2015 07:45:17 +0000” let dateFormatter = DateFormatter() dateFormatter.dateFormat = “EEE, dd MMM yyyy hh:mm:ss +zzzz” dateFormatter.locale = Locale.init(identifier: “en_GB”) … Read more

How to get the time in am/pm format iphone NSDateFormatter?

If your time format is in 24hrs please use “HH:mm:ss” format. Please test the code and let me know if it is working for you. I have tested and it is returning “4 PM” NSString *dats1 = @”16:00:00″; NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; [dateFormatter3 setDateStyle:NSDateFormatterMediumStyle]; [dateFormatter3 setDateFormat:@”HH:mm:ss”]; NSDate *date1 = [dateFormatter3 dateFromString:dats1]; NSLog(@”date1 … Read more

Ordinal Month-day Suffix Option for NSDateFormatter setDateFormat

None of these answers were as aesthetically pleasing as what I’m using, so I thought I would share: Swift 3: func daySuffix(from date: Date) -> String { let calendar = Calendar.current let dayOfMonth = calendar.component(.day, from: date) switch dayOfMonth { case 1, 21, 31: return “st” case 2, 22: return “nd” case 3, 23: return … Read more

Swift 3.0 : Convert server UTC time to local time and vice-versa

I don’t know what’s wrong with your code.But looks too much unnecessary things are there like you’re setting calendar, fetching some elements from string. Here is my small version of UTCToLocal and localToUTC function. But for that you need to pass string in specific format. Cause I’ve forcly unwrapped date objects. But you can use … Read more