Reputation: 749
I am trying to detect the time format is on 12 hour/24 hour format. Based on that i am setting time in calender.
I used the following code to detect 24 hour format on device in viewDidLoad Method.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setLocale:[NSLocale currentLocale]];
[formatter setDateStyle:NSDateFormatterNoStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
NSString *dateString = [formatter stringFromDate:[NSDate date]];
NSRange amRange = [dateString rangeOfString:[formatter AMSymbol]];
NSRange pmRange = [dateString rangeOfString:[formatter PMSymbol]];
is24h = (amRange.location == NSNotFound && pmRange.location == NSNotFound);
After that i am checking as follows
if (is24h == TRUE) {
} else {
}
But i am always getting is24h value is false. I am able to set time in calender if the device set in 12 hour format. But i can't able to set time if it is in 24 hour format.
Can you please tell me where i need to change.
Upvotes: 1
Views: 2122
Reputation: 749
Hi now my code is working fine. I wrote the code as follows.
-(BOOL)timeIs24HourFormat{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setLocale:[NSLocale currentLocale]];
[formatter setDateStyle:NSDateFormatterNoStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
NSString *dateString = [formatter stringFromDate:[NSDate date]];
NSRange amRange = [dateString rangeOfString:[formatter AMSymbol]];
NSRange pmRange = [dateString rangeOfString:[formatter PMSymbol]];
is24h = (amRange.location == NSNotFound && pmRange.location == NSNotFound);
return is24h;
}
Upvotes: 1
Reputation: 9836
Use this method to determine time format
- (BOOL)timeIs12HourFormat {
NSString *formatStringForHours = [NSDateFormatter dateFormatFromTemplate:@"j" options:0 locale:[NSLocale currentLocale]];
NSRange containsA = [formatStringForHours rangeOfString:@"a"];
BOOL hasAMPM = containsA.location != NSNotFound;
return hasAMPM;
}
according to the unicode date format guide j
means
This is a special-purpose symbol. It must not occur in pattern or skeleton data. Instead, it is reserved for use in skeletons passed to APIs doing flexible date pattern generation. In such a context, it requests the preferred hour format for the locale (h, H, K, or k), as determined by whether h, H, K, or k is used in the standard short time format for the locale. In the implementation of such an API, 'j' must be replaced by h, H, K, or k before beginning a match against availableFormats data. Note that use of 'j' in a skeleton passed to an API is the only way to have a skeleton request a locale's preferred time cycle type (12-hour or 24-hour).
EDIT
This will first get the formatString from currentLocale of the device and then it will check if "a" is exists then it is 12 Hour format and if not then it is 24 hour format.
Upvotes: 9
Reputation: 1501
If you want a 12 hour clock then set currentDate as follows:
[currentDate setDateFormat:@"yyyy-MM-dd h:mm:ss a"];
If you want a 24 hour clock then set currentDate as follows:
[currentDate setDateFormat:@"yyyy-MM-dd HH:mm"];
Upvotes: -2