0

Why does this code return 12:00:05 am (not 00:00:05)? (System time format is set to 12-Hour.)

How can I get what I want without hardcoding new locale? (because users' prefs should stay users' prefs and there is no guarantee/control that locale will provide exact time format for me.

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"HH:mm:ss"];
NSDate *date = [[NSDate alloc] init];
NSString *currentDateAndTime = [dateFormatter stringFromDate:date];
NSLog(@"%@", currentDateAndTime);

People say that NSDate stores seconds and does not store format. So it must depend on format set by NSDateFormatter, not on system time format, but it does not. Why?

UPD: I've got solution, but still do not have an answer Why? — any ideas?

Specified current locale and got what I need:

[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:[[NSLocale currentLocale] localeIdentifier]]];

UPD 2: My KK solution does not work

When time crosses noon mark it returns 00:00:00. Like it's crossed midnight

Solved by setting locale to en_GB

Don't know how it works, so not sure if it's going to work properly in the future. As for me it would be better to know if time set to 24 hour format. Or to see the locale's insides

Community
  • 1
  • 1
Denis
  • 940
  • 12
  • 16
  • Isn't that correct 12-hour format? Normally midnight is 12:00am – Jason Dec 03 '14 at 21:18
  • @Jason sorry, I've missed seconds in `dateFormater` – Denis Dec 03 '14 at 21:23
  • When I try that code with a date that's just after midnight, I **do** get an hour of "00" instead of "12". That is, the problem you describe cannot be reproduced with the code you have posted. – Tom Harrington Dec 03 '14 at 21:23
  • @Jason, `HH` is 24-hour time and should return "00" for midnight. To get a 12 at midnight you'd need `hh'. – Tom Harrington Dec 03 '14 at 21:24
  • Which locale do you run this under. HH should force 24 hour format, but since it doesn't, this might be a locale problem. When you don't specify a locale, the formatter will use the system specific one. – driis Dec 03 '14 at 21:25
  • @driis I don't know why but it starts working correctly if I specify **any** locale. So I've added `[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:[[NSLocale currentLocale] localeIdentifier]]];` and got `00:40:02` (my current time). – Denis Dec 03 '14 at 21:41
  • If you're getting `00:40:02`, what problem are you trying to solve? Your question asked why you were getting `12` instead of `00`, but now you say you're getting `00`, so what is it that you need to know here? – Tom Harrington Dec 03 '14 at 21:44
  • @TomHarrington I thought that I control format by NSDateFormatter, but appears that do not. The problem is that I do not understand why setting current locale to current locale effect something (sic!). – Denis Dec 03 '14 at 21:49
  • 2
    From the Apple docs: In all cases, you should consider that formatters default to using the user’s locale (currentLocale) superimposed with the user’s preference settings. If you want to use the user’s locale but without their individual settings, you can get the locale id from the current user locale (localeIdentifier) and make a new "standard” locale with that, then set the standard locale as the formatter’s locale – Mark S. Dec 03 '14 at 21:52
  • @TomHarrington I need to get current time in 24 hour format no matter what user's set – Denis Dec 03 '14 at 21:52
  • 1
    You are apparently being bit by the locale "feature". – Hot Licks Dec 03 '14 at 22:00
  • @MarkS. thank you, I did not get into these words before. I think because I has not changed my own system region preferences and thought that I use "virgin" locale. Looks like not. So thank you for this quotation – Denis Dec 03 '14 at 22:00
  • 1
    It should be noted that this problem only occurs when the phone's 12/24 setting in in contradiction of the default locale's 12/24 preference. – Hot Licks Dec 03 '14 at 22:09

2 Answers2

1

KK — is the key!

Does not work properly, see UPD 2 in the question

Thanks to someone who deleted his answer. He mentioned K key for skeleton — yes. It works without specifying locale.

So I use this mask and get what I need no matter what are the settings:

[dateFormatter setDateFormat:@"KK:mm:ss"];

From Date Format Patterns

Community
  • 1
  • 1
Denis
  • 940
  • 12
  • 16
0

Here is the way to set date and time:-

1) Go to the System Preference->Date & Time->Open Language & Region Check the Time format to 24-Hour Time accordingly.

2) Now write the below code to extract the specified date and time.

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSDate *date = [NSDate date];
//Below date formatter will set your date and time based on your default System Pref. settings
[dateFormatter setDateStyle:NSDateFormatterFullStyle];
[dateFormatter setTimeStyle:NSDateFormatterLongStyle];
NSLog(@"%@",[dateFormatter stringFromDate:date]);
Hussain Shabbir
  • 14,801
  • 5
  • 40
  • 56