-[NSDateFormatter dateFromString:] returns nil

2019-02-25 14:31发布

问题:

    NSString *lower = [NSString stringWithFormat:@"%@",[newDates objectAtIndex:0]];
    NSString *higher = [NSString stringWithFormat:@"%@",[newDates objectAtIndex:[newDates count]-1]];
    NSLog(@"%@",lower);
    NSLog(@"%@",higher);

    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"yyyy-MM-dd"];
    NSDate *dtLower = [df dateFromString:lower];

    NSDate *dtHigher = [df dateFromString:higher];
    [df release];
    NSDateFormatter *df1 = [[NSDateFormatter alloc] init];
    [df1 setDateFormat:@"yyyy-MM-dd"];
    NSString * lowerDate = [df1 stringFromDate:dtLower];
    NSString * higherDate = [df1 stringFromDate:dtHigher];
    UILabel *dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 31, self.view.frame.size.width, 30)];
    [dateLabel setTextColor:[UIColor grayColor]];
    dateLabel.text = [NSString stringWithFormat:@"%@ - %@",lowerDate,higherDate];
    [self.view addSubview:dateLabel];

Output of lower and higher in Console is:

2011-05-23 14:55:52.767 Vocab[4225:207] 2011-05-23 03:58:22
2011-05-23 14:55:53.781 Vocab[4225:207] 2011-05-23 07:14:56

Here the above code does not work for me. The value for higher and lower are not null but when I try converting it into NSDate using NSDateFormatter then it returns nil.

So dtLower and dtHigher return nil

What could be wrong?

回答1:

Try setting [df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];



回答2:

 [df setDateFormat:@"yyyy-MM-dd hh:mm:ss"];

Your first date format is wrong.



回答3:

If you're working with user-visible dates, you should avoid setting a date format string as it's very hard to predict how your format string will be expressed in all possible user configurations. Rather, you should try and limit yourself to setting date and time styles (via -[NSDateFormatter setDateStyle:] and -[NSDateFormatter setTimeStyle:]).

If you're working with fixed-format dates, you should first set the locale of the date formatter to something appropriate for your fixed format. In most cases the best locale to choose is en_US_POSIX, a locale that's specifically designed to yield US English results regardless of both user and system preferences. en_US_POSIX is also invariant in time and between machines (en_US_POSIX works the same on iOS as it does on OSX, and as it it does on other platforms).

Once you've set en_US_POSIX as the locale of the date formatter, you can then set the date format string and the date formatter will behave consistently for all users.

The above info and more can be found in Apple's Technical Q&A QA1480

Here's a snippet of code which implements the recommendation from the above Technical Note:

static NSDateFormatter* dateFormatter = nil;
if (!dateFormatter) 
{
   dateFormatter = [NSDateFormatter new];

   NSLocale *enUSPOSIXLocale = [[NSLocale alloc]  
                                 initWithLocaleIdentifier:@"en_US_POSIX"];

   [dateFormatter setLocale:enUSPOSIXLocale];
   [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
   dateFormatter.dateFormat = @"EEE, dd MMM yyyy HH:mm:ss +0000";
}