iOS DateFormatter返回Null

时间:2016-02-04 13:42:15

标签: ios nsdateformatter

我想得到日期/月份和时间:分钟来自" 12/10/2015 6:11:04 PM"格式日期。我来自土耳其,所以我们的日期格式为23小时。当我使用下面的代码时,它会返回日期" nil"。

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"tr_TR_POSIX"]];
[dateFormatter setDateFormat:@"d/m/yyyy h:mm:ss a"];
NSDate *date = [dateFormatter dateFromString:@"12/10/2015 6:11:04 PM"];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:(NSCalendarUnitHour | NSCalendarUnitMinute) fromDate:date];
NSInteger hour = [components hour];
NSInteger minute = [components minute];

1 个答案:

答案 0 :(得分:0)

更改dateformat,然后尝试

[dateFormatter setDateFormat:@"dd/MM/yyyy h:mm:ss a"];