How to Deal With the Nsdateformatter Locale "Feechur"

What is the best way to deal with the NSDateFormatter locale feechur?

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", presumably because of some missing data structure in the object. Changing the init to self = [self init]; causes the NSDateFormatter initialization to occur, and setLocale is happy again.

Here is the "final" source for the category's .m:

#import "NSDateFormatter+Locale.h"

@implementation NSDateFormatter (Locale)

- (id)initWithSafeLocale {
static NSLocale* en_US_POSIX = nil;
self = [self init];
if (en_US_POSIX == nil) {
en_US_POSIX = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
}
[self setLocale:en_US_POSIX];
return self;
}

@end

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 = NSLocale(localeIdentifier: "en_US_POSIX")

// Swift 3:
formatter.locale = Locale(identifier: "en_US_POSIX")

NSDateFormatter Locale

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSLocale *locale = [[NSLocale alloc]
initWithLocaleIdentifier:@"he"];
[dateFormatter setLocale:locale];

NSDateFormatter with format string outputs time with short style

You should set timezone and locale to your date formatter, try it like this:

static var InternalDateFormatter: NSDateFormatter = {
let dateFormatter: NSDateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyyMMdd HH':'mm"
dateFormatter.timeZone = NSTimeZone.localTimeZone()
dateFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
return dateFormatter
}()

Using en_US_POSIX locale ID will always give you am-pm US time standard.

But you can choose any locale ID - https://gist.github.com/jacobbubu/1836273

NSDateFormatter - setLocale ignored?

The issue is simply how you are printing the date using NSLog(). This will call [NSDate description] which will use the user's default locale.

From the docs (of descriptionWithLocale):

If you pass nil, NSDate formats the date in the same way as the
description property.

On OS X v10.4 and earlier, this parameter was an NSDictionary object.
If you pass in an NSDictionary object on OS X v10.5, NSDate uses the
default user locale—the same as if you passed in [NSLocale
currentLocale]
.

Instead you should use the date formatter to print the formatted string.

NSDateFormatter, am I doing something wrong or is this a bug?

Using the code you posted on both the simulator and a phone with the 2.1 firmware and 24-hour time set to off, I never had an AM/PM appended to dateStr when I do:

NSLog(@"%@", dateStr);

Are you doing anything else with dateStr that you didn't post here? How are you checking the value?

Follow up

Try turning the am/pm setting on then off. I didn't have the problem either, until I did that. I am printing it out the same way you are.

Okay, I see it when I do this also. It's gotta be a bug. I recommend you file a bug report and just check for and filter out the unwanted characters in the meantime.

Why does my NSDateFormatter sometimes return an a.m. or p.m. with yyyyMMddHHmmssSSS?

The problem you are describing is a known bug. Check out some discussion on the problem on stackoverflow, and you can find some possible work-arounds there.

Here's an excerpt of huyz's explaination of the bug:

The problem comes from NSDateFormatter somehow “getting stuck” in the 12 or 24-hour time mode that the user has manually selected. So if a French user manually selects 12-hour mode, and the application requested NSDateFormatter to output time with the 24-hour format “HHmm”, it would actually receive time in a 12-hour format, e.g. “01:00 PM”, as if the application had instead requested “hhmm aa”. The reverse would happen if a US user manually selected 24-hour mode: outputting time with the 12-hour format “hhmm aa” would actually get you time in the 24-hour format instead, e.g. “17:00″.

NSDateFormatter printing current date in format M/yyyy hh:mm in any locale

If you want to format a date in a specific format, then why are you using dateFormatFromTemplate:? The purpose of that method is to give you a locale specific format from a general format.

Just use normal NSDateFormatter functionality:

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"M/yyyy hh:mm"];
NSDate *now = [NSDate date];
NSString *formattedDate = [formatter stringFromDate:now];


Related Topics



Leave a reply



Submit