Objective c NSDateFormatter返回零
有人能告诉我为什么此代码段在我的日志文件中返回nil:Objective c NSDateFormatter返回零,objective-c,nsdateformatter,Objective C,Nsdateformatter,有人能告诉我为什么此代码段在我的日志文件中返回nil: NSString *dateString = [[NSString alloc] initWithString: @"2011-08-23 15:00"]; NSLocale *englishLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en"]; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] ini
NSString *dateString = [[NSString alloc] initWithString: @"2011-08-23 15:00"];
NSLocale *englishLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"YYYY-mm-dd HH:MM"];
[dateFormatter setLocale:englishLocale];
NSLog(@"%@",[dateFormatter dateFromString:dateString]);
非常感谢。您应该将“HH:MM”改为“HH:MM”
NSString *dateString = [[NSString alloc] initWithString: @"2011-08-23 15:00"];
NSLocale *englishLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//HH:MM -> HH:mm
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
[dateFormatter setLocale:englishLocale];
NSLog(@"%@",[dateFormatter dateFromString:dateString]);
检查我的变体:
NSString *dateString = [[NSString alloc] initWithString: @"2011-08-23 15:00"];
NSLocale *englishLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//HH:MM -> HH:mm
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
[dateFormatter setLocale:englishLocale];
NSLog(@"%@",[dateFormatter dateFromString:dateString]);
如您所见,我已将日期格式更改为:@“yyy-MM-dd HH:MM”
所有格式都可以在这里找到:
关于更多的日期格式这是因为NSLocale设置吗?现在是夏天,生活很轻松。。。(即是)对不起。我搞错了。我也修改了。