I finally got this to work by converting every date and time using the "en_US_POSIX" locale. I know, it's not truly what I want but it works across different region and time formats. I still have to figure out how to express these dates and times purely based on the user's region and time formats. Maybe, for another day.
NSDateFormatter based on the time settings of iPhone
-
28-06-2023 - |
Pergunta
I have the NSDateFormatter
for displaying time as h:mm
to display time as 7:50 PM for example.
It works fine for phones with time showing as 5:55 PM
etc. but there are phones in different countries with time showing on the Status Bar as 05:55 PM and with that setting the NSDateTimeFormatter
with h:mm
seem to return (null).
Are there any workarounds or is there a best way to handle these situations? I was thinking off to check for nil and then retry NSDateFormatter
with hh:mm
but that sounds ugly to me. I wanted to make sure I do it right.
Solução 2
Outras dicas
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
NSLog(@"%@", [dateFormatter stringFromDate:[NSDate date]]);
For language German and region format Germany, it logs 01:46
, while for English and United States it logs 1:46 AM
. And I checked: it will print 13:52
and 1:52 PM
correctly for times after noon.