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/

Convert string to date in Swift

Convert the ISO8601 string to date let isoDate = “2016-04-14T10:44:00+0000” let dateFormatter = DateFormatter() dateFormatter.locale = Locale(identifier: “en_US_POSIX”) // set locale to reliable US_POSIX dateFormatter.dateFormat = “yyyy-MM-dd’T’HH:mm:ssZ” let date = dateFormatter.date(from:isoDate)! Get the date components for year, month, day and hour from the date let calendar = Calendar.current let components = calendar.dateComponents([.year, .month, .day, .hour], … Read more

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

NSString to NSDate

You can’t invent format string syntax and expect it to work; you need to actually use a documented format. (Seriously, “MM” meaning “month”, “minute” and “GMT offset minutes” all at the same time?) As the documentation points out, the 10.4 formatters use Unicode format strings. Try “yyyy-MM-dd HH:mm:ss ZZZ” instead. Also, Objective-C source is ASCII. … Read more

DateFormatter’s returns nil for specific date strings without time in Swift

You just need to set your date formatter calendar property: Xcode 8.2.1 • Swift 3.0.2 let dateFormatter = DateFormatter() dateFormatter.calendar = Calendar(identifier: .iso8601) dateFormatter.locale = Locale(identifier: “es”) dateFormatter.dateFormat = “dd ‘de’ MMMM” dateFormatter.date(from: “8 de octubre”) // “Oct 8, 2000, 1:00 AM”

Date Format in Swift

This may be useful for who want to use dateformater.dateformat; if you want 12.09.18 you use dateformater.dateformat = “dd.MM.yy” Wednesday, Sep 12, 2018 –> EEEE, MMM d, yyyy 09/12/2018 –> MM/dd/yyyy 09-12-2018 14:11 –> MM-dd-yyyy HH:mm Sep 12, 2:11 PM –> MMM d, h:mm a September 2018 –> MMMM yyyy Sep 12, 2018 –> MMM … Read more

How do I get the current Date in short format in Swift

Xcode 11 or later • Swift 5.1 or later extension TimeZone { static let gmt = TimeZone(secondsFromGMT: 0)! } extension Locale { static let ptBR = Locale(identifier: “pt_BR”) } extension Formatter { static let date = DateFormatter() } extension Date { func localizedDescription(date dateStyle: DateFormatter.Style = .medium, time timeStyle: DateFormatter.Style = .medium, in timeZone: TimeZone … Read more

Difference between ‘YYYY’ and ‘yyyy’ in NSDateFormatter

Also when using a date format string using the correct format is important. @”YYYY” is week-based calendar year. @”yyyy” is ordinary calendar year. You can go through the whole blog, its a good to give it a look https://web.archive.org/web/20150423093107/http://realmacsoftware.com/blog/working-with-date-and-time http://realmacsoftware.com/blog/working-with-date-and-time (dead link)