NSDate and NSDateFormatter issues

12,730

The problem is that the input date is in the "yyyy-MM-dd" format, but the date formatter you're using with dateFromString is formatted "MMMM d, yyyy". You'll need to try parsing with both formats if you're desiring both formats to be accepted.

For example:

[dateFormatter setDateFormat:@"MMMM d, yyyy"];
NSDate *date = [dateFormatter dateFromString:cDate];
if (date == nil) {
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    date = [dateFormatter dateFromString:cDate];
    if (date == nil) {
        // Handle the situation where the date string could not be parsed
    }
}
Share:
12,730
Roman
Author by

Roman

Updated on June 04, 2022

Comments

  • Roman
    Roman almost 2 years

    I'm having slight difficulty in understanding why the following code is crashing an app of mine:

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"MMMM d, yyyy"];
    NSDate *date = [dateFormatter dateFromString:cDate];
    datePicker.date = date;
    NSString *dateStr = [dateFormatter stringFromDate:date]; 
    [dateLabel setText:dateStr];
    [dateFormatter release];
    

    If I comment the above out, app is fine. Also if I change the date format to the following no crash happens:

    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    

    In my UIDatePicker delegate, I have repeated code that looks like the following (and works great):

    -(IBAction)datePickerValueChanged:(id)sender 
    {
        NSDate *date = [datePicker date];       
        NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
        [dateFormatter setDateFormat:@"MMMM d, yyyy"];
        NSString *dateStr = [dateFormatter stringFromDate:date]; 
        [dateLabel setText:dateStr]; 
    }
    

    The error I get is the following:

    *** Assertion failure in -[UIDatePickerView _updateBitsForDate:andReload:animateIfNeeded:], /SourceCache/UIKit/UIKit-747.38/UIDatePicker.m:892
    
    *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: date'
    
  • Roman
    Roman about 15 years
    I've ensured that's the case, although I think the formatting of cDate is what's breaking it. It is set to: 1972-01-01. That worked fine for when my format string was "yyyy-MM-dd". Now that it's "MMMM d, yyyy" it crashes.
  • Ecton
    Ecton about 15 years
    What is the value of "date" before assigning to the date picker? Is it outside of the minimum and maximum date values for the picker?
  • Roman
    Roman about 15 years
    I believe the issue is NSDateFormatter doesn't know how to internet something in the format of say: 1990-01-01 and translate it to setDateFormat:@"MMMM d, yyyy", it is capable of doing it when the format string is "yyyy-MM-dd".
  • Ecton
    Ecton about 15 years
    The problem is that the date formatter's format is being set to MMMM d, yyyy, but you're giving it a date in a different format. My guess is that if you were to answer my question, you would notice that "date" was an invalid value. You'll need to parse the date with a matching format string.
  • Ecton
    Ecton about 15 years
    If your date is "1972-01-01", it will only be able to be parsed by a format of "yyyy-MM-dd". Thus, you must parse using that format. If you're wanting to accept two formats, you'll have to try once with one format, and then try to parse again using the other format.