1

So I've got an application in which some data is returned from a web service and a UIDatePicker is adjusted accordingly. The date is returned in the following format:

12-05-2013 16:05 (dd-mm-yyyy hh:mm)

The time is stored in 24 hour format, however the UIDatePicker is either 12 hour or 24 hour depending upon the settings on the device. The data comes in a JSON array and is the key potdd. The date picker I'm setting is called potddatepicker.

NSDateFormatter *inputFormat = [[NSDateFormatter alloc] init];
[inputFormat setDateFormat:@"dd-MM-yyy hh:mm"];
NSDate *inputDate = [inputFormat dateFromString: [jsonObjects objectForKey:@"potdd"]];

NSDate *potdd = inputDate;
[_potddatepicker setDate:potdd];

If the date is set before 12:00 then it works fine. If however the date is store in 24 hour time after 12 then it crashes with the following exception:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: date'

I don't particularly want to force the time into 12 hour format just for the datepicker and then force it back into 24 hour when it comes out because in some locals it may be used in a 24 hour date picker. Anybody know how I can deal with this? I can't change the format that the time is stored in the database.

Thanks!

Compy
  • 1,157
  • 1
  • 12
  • 24
  • Did you inspect `inputDate` to see if it's got the value you expect? – jscs Jun 29 '13 at 20:35
  • @GradyPlayer The problem with the answer to that duplicate is that is is wrong. See the comments below that answer. That is why I pointed out the need to use the special `en_US_POSIX` locale in my answer to this question. – rmaddy Jun 29 '13 at 21:09
  • @rmaddy the op's formatter is slightly more flawed, but the main point of h vs H is the same. – Grady Player Jun 29 '13 at 21:18
  • @GradyPlayer I assume you mean the missing `y`. I just noticed that and updated my answer to use `yyyy` and not `yyy`. – rmaddy Jun 29 '13 at 21:23
  • @tblue == Jean-Michel Basquiat? – Grady Player Jun 29 '13 at 23:17

2 Answers2

7

Your error has nothing to do with the date picker.

You are using the wrong date format to parse the string. You need:

[inputFormat setDateFormat:@"dd-MM-yyyy HH:mm"]; // HH is 24-hour, hh is 12-hour

Also, since you are working with a fixed format, you should set the date formatter's locale to en_US_POSIX.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
3

I suspect you just want HH instead of hh in your input format. HH is 24 hour, hh is 12 hour. I suspect you also want MM instead of the first mm. (MM is a two digit month specifier; mm is a two digit minute specifier. You don't want to set the minutes twice!)

So the value 12-05-2013 16:05 is invalid for a format of dd-mm-yyyy hh:mm - but it's valid for dd-MM-yyyy HH:mm.

See the Unicode Date Format Patterns documentation for more details - basically you'll need to be careful that your pattern matches your input data. I would hope that the UI settings of the picker are independent of your JSON data input format, but I'm not sufficiently up on iOS development to say for sure.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    the date picker will return an NSDate, which is basically a Obj-C wrapper for a `double` of seconds since the reference date, and has nothing to do with the formatted representation, so yes they are totally separate. – Grady Player Jun 29 '13 at 20:33
  • @GradyPlayer: That's what I was hoping :) – Jon Skeet Jun 29 '13 at 20:46