字符串转换成NSDate类型的 为nil解决方法
方法一 通过下列函数来解决 但是得到的日期会改变 修改方法fix
- (NSDate *)timeForString:(NSString *)string {
NSMutableString *timeString = [[NSMutableString alloc] initWithString:string];
[timeString setString:[timeString stringByReplacingOccurrencesOfString:@"/Date(" withString:@""]];
[timeString setString:[timeString stringByReplacingOccurrencesOfString:@")/" withString:@""]];
[timeString setString:[timeString substringToIndex:timeString.length - 3]];
return [NSDate dateWithTimeIntervalSince1970:[timeString longLongValue]];
}
方法二 已解决
NSString *time = [_userDic objectForKey:USERAGE];
NSDateFormatter *formatter1 = [[NSDateFormatter alloc] init];
[formatter1 setDateFormat:@"yyyyMMdd"];
[formatter1 setTimeZone:[NSTimeZone timeZoneWithName:@"GTM"]];//GTM,格林尼治时区
NSDate *date = [formatter1 dateFromString:time];
NSTimeInterval dateDiff = [date timeIntervalSinceNow];
int age= fabs(trunc(dateDiff / (60 * 60 * 24)) / 365);
NSDateFormatter *formatter1 = [[NSDateFormatter alloc] init];
[formatter1 setDateFormat:@"yyyyMMdd"];
[formatter1 setTimeZone:[NSTimeZone timeZoneWithName:@"GTM"]];//GTM,格林尼治时区
NSDate *date = [formatter1 dateFromString:time];
NSTimeInterval dateDiff = [date timeIntervalSinceNow];
int age= fabs(trunc(dateDiff / (60 * 60 * 24)) / 365);
cell.value.text = [NSString stringWithFormat:@"%d", age];
posted on 2015-09-30 17:30 taiyang2014 阅读(648) 评论(0) 编辑 收藏 举报