swift NSDateFormatter not working

The 24-hour format is “HH”, not “hh”. The reason that it works in the Playground may be that user defined settings can override the 12/24-format choice, compare What is the best way to deal with the NSDateFormatter locale “feechur”?. To be on the safe side, set the “en_US_POSIX” locale for the date formatter: formatter.locale = … Read more

What is the best way to deal with the NSDateFormatter locale “feature”?

Duh!! Sometimes you have an “Aha!!” moment, sometimes it’s more of a “Duh!!” This is the latter. In the category for initWithSafeLocale the “super” init was coded as self = [super init];. This inits the SUPERCLASS of NSDateFormatter but does not init the NSDateFormatter object itself. Apparently when this initialization is skipped, setLocale “bounces off”, … Read more

NSDateFormatter: Date according to currentLocale, without Year

I think you need to take a look at: + (NSString *)dateFormatFromTemplate:(NSString *)template options:(NSUInteger)opts locale:(NSLocale *)locale As per the docs: Returns a localized date format string representing the given date format components arranged appropriately for the specified locale. Return Value A localized date format string representing the date format components given in template, arranged appropriately … Read more

NSDateFormatter and current language in iOS11

This isn’t a problem with NSDateFormatter, it’s a change in how iOS 11 supports localization. Under iOS 11, [NSLocale currentLocale] only returns languages supported by your app’s localizations. If your app only supports English (as the base localization), then no matter what language the user selects on the device, currentLocale will always return English. Under … Read more

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 NSDate in to relative format as “Today”,”Yesterday”,”a week ago”,”a month ago”,”a year ago”?

For simplicity I’m assuming that the dates you are formatting are all in the past (no “tomorrow” or “next week”). It’s not that it can’t be done but it would be more cases to deal with and more strings to return. You can use components:fromDate:toDate:options: with whatever combination of date components you are looking for … Read more

NSDateFormatter returning nil in OS 4.0

I found out it works if you do it this way (see below). The key is using the method: – [NSDateFormatter getObjectValue:forString:range:error:] instead of -[NSDateFormatter dateFromString] The complete code: + (NSDate *)parseRFC3339Date:(NSString *)dateString { NSDateFormatter *rfc3339TimestampFormatterWithTimeZone = [[NSDateFormatter alloc] init]; [rfc3339TimestampFormatterWithTimeZone setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@”en_US_POSIX”] autorelease]]; [rfc3339TimestampFormatterWithTimeZone setDateFormat:@”yyyy-MM-dd’T’HH:mm:ssZ”]; NSDate *theDate = nil; NSError *error = nil; … Read more