2

I know there are many NSDateFormatter questions on here, so if I duplicate, I'm sorry. I just couldn't find anything that was quite what Im asking.

From all the questions here on SO, I have come to the conclusion that -[NSDateFormatter dateFromString:] will always return NULL if your formatter object doesn't have the correct date format. How do you get a date from a string if you don't know the format? I'm trying to get a date from a UITextField.

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setLocale:[NSLocale currentLocale]];
[formatter setLenient:YES];
NSDate *tempDate = [formatter dateFromString:self.birthdayTxtfld.text];
self.currentCustomer.birthday = ([self.birthdayTxtfld.text isEqualToString:@""]) ? NULL : tempDate;
[formatter release];

tempDate is always NULL.

Costique
  • 23,712
  • 4
  • 76
  • 79
sasquatch
  • 285
  • 2
  • 13

2 Answers2

3

I think your taking the wrong approach. I would on the other hand restrict and format the UITextField so the user has to enter the date in a specific format. Or just use a date picker. There are just way too may different inputs the user could give you.

Or you can read through this: NSDate

Another option is to create a list of accepted date formats: #define DATEFORMATS @[@"MM/dd/yyyy", @"MM/dd/yy",... Then Have a method that you pass the date string to and check if you can format it:

+ (NSDateFormatter*)getDateFormat:(NSString*)dateString {

    NSArray *dateFormats = DATEFORMATS;
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    NSDate *date = nil;
    for (NSString *dateFormat in dateFormats) {
        [formatter setDateFormat:dateFormat];
        date = [formatter dateFromString:dateString];
        if (date) {
            return formatter;
        }
    }
    return nil;
}

If you get nil its not a date or its in a format you don't support. Otherwise you will have the correct format you need. You can switch this around to return the date instead of the format. I have it this way because I needed the format not the date for a project.

Jaybit
  • 1,864
  • 1
  • 13
  • 20
  • 1
    +1 Parsing an unknown string is definitely the hard way. Simply set the `inputView` property of the textfield to be a view that contains a picker. I have posted [an example of such a class in my answer here.](http://stackoverflow.com/a/8978326/927947) It would be easy to add an `NSDateFormatter` to the class to format the string displayed in the textfield, and read it's value later. – NJones Feb 09 '12 at 20:21
  • @sasquatch Given the abundance and complexity of possible date formats, it's _practically impossible_ to guess the format of the date. Just use `UIDatePicker` instead. – Costique Feb 10 '12 at 09:38
  • this does make the most sense. I hadn't thought of that, but reading this answer was a "doh!" moment. Thanks guys, it is unfortunate that date formatter doesn't have a contingency for this though – sasquatch Feb 10 '12 at 15:55
1

While I'm in agreement with @Jaybit that you probably need to ditch the text box and use a better input, the answer to this specific question lies in some crafty string parsing. Whenever you are doing string parsing, RegEx is your friend. Web developers end up having to do this crap all the time. This example is in JavaScript, but the RegEx ought to be portable enough that it works in ObjC:

http://www.codingoptimist.com/2009/07/using-javascript-and-regex-to-parse.html

You can do this with RegExKit or NSRegularExpression

slf
  • 22,595
  • 11
  • 77
  • 101
  • While I'm a fan of the power and usefulness of Regular Expressions, I don't think this is the place for them. That would be really complex when there are much easier solutions, as mentioned in the accepted answer. – sasquatch Feb 10 '12 at 19:33
  • I'm ecstatic that someone actually looked at my blog post and even more so that they thought it was worth linking. Thanks slf! – Jerry Feb 15 '12 at 05:16