Reputation: 221
I have an application in which I let users choose their language, after choosing the language it will also change the dates and time according to the the country's date format.
I do so by a NSDateFormatter.- [dateFormatter setDateFormat:.....]; for english-US - I use "h:mm a" - to show am/pm settings.
PROBLEM IS: the am pm are only shown if my iPhone is on 12 hours mode, I guess it's because it automatically checks for the iPhone's time format, is there a way to override this? that the am/pm will show as well in 24 mode.
Upvotes: 1
Views: 3742
Reputation: 10116
Assuming you need to customize your date format for a user-facing string because the presets for dateStyle
and timeStyle
don't cut it for you, the appropriate way to do this is to use setLocalizedDateFormatFromTemplate
with "j" or "jj" for the hours depending on whether you want zero padding (in regions that use that style, presumably — so far it seems to always leave out zero padding but I am guessing it is region-dependent).
let dateFormatter = DateFormatter()
dateFormatter.setLocalizedDateFormatFromTemplate("jj:mm")
If the user is in a region that uses 24 hour time by default and/or they have 24 hour time turned on (via the switch in iOS Settings) it will output strings like "17:53". Otherwise it will output strings like "5:53 PM".
Note if you are overriding locale
on your formatter be sure to set it before calling setLocalizedDateFormatFromTemplate
.
The template approach will automatically localize your strings to use the appropriate styling.
More information can be found in the documentation under "Working With User-Visible Representations of Dates and Times".
Upvotes: 0
Reputation: 11
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setLocale:[NSLocale currentLocale]];
[formatter setDateStyle:NSDateFormatterNoStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:@"hh:mm:ss a"];
NSString *date = [formatter stringFromDate:[NSDate date]];
NSArray *foo1 = [date componentsSeparatedByString:@" "];
NSRange amRange = [date rangeOfString:[formatter AMSymbol]];
NSRange pmRange = [date rangeOfString:[formatter PMSymbol]];
BOOL is24h = (amRange.location == NSNotFound && pmRange.location == NSNotFound);
NSArray *foo2 = [[foo1 objectAtIndex:0] componentsSeparatedByString:@":"];
int hour = [[foo2 objectAtIndex:0] intValue];
int minute = [[foo2 objectAtIndex:1] intValue];
int second = [[foo2 objectAtIndex:2] intValue];
BOOL isAM = NO;
if (is24h) {
if (hour >= 12) {
isAM = NO;
} else {
isAM = YES;
}
} else {
isAM = [[foo1 objectAtIndex:1] isEqualToString:@"AM"];
}
Upvotes: 0
Reputation: 405
This works for me:
dateFormatter.dateFormat = @"H:mm a";
Example:
// Set up formatter
NSDateFormatter *dateFormatter;
dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"H:mm a";
// Print test date
NSDate *date = [NSDate dateWithTimeIntervalSinceNow:8900];
NSLog(@"%@", [dateFormatter stringFromDate:date]);
Output:
14:23 PM
Upvotes: 7