iOS DateFormatter返回Null

问题描述 投票:-2回答:1

我希望从“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];
ios nsdateformatter
1个回答
0
投票

更改日期格式,然后尝试

[dateFormatter setDateFormat:@"dd/MM/yyyy h:mm:ss a"];
© www.soinside.com 2019 - 2024. All rights reserved.