IOS time in 24 hours format

I want to get the current time and analyze it in the format Stringin 24 hours, for example 13:30.

This is my code:

NSDate *date = [NSDate date];
NSDateFormatter *formatter = [NSDateFormatter new];
[formatter setDateFormat:@"HH:mm"];
NSString *timeString = [formatter stringFromDate:date];

It works on iOS simulator, but on mine iPadI get 3:30 PM.

I played with different formats, but still the same result.

Any idea why?

Thanks in advance.

+4
source share
2 answers

The problem is that the local user user is set to 12 hour format. Set the local value en_US_POSIXto overload user preference.

dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
+2
source

24- .

, NSDateFormatter, . NSDateFormatter Locale.

@mtb . , .

+1

Source: https://habr.com/ru/post/1628998/


All Articles