objective-c字符串转化成NSDate类型,计算与当前时间的相差,月数,天数
2018-07-20 来源:open-open
NSString *dateStr=[dic objectForKey:@"date"];// 2012-05-17 11:23:23 NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSUInteger unitFlags = NSMonthCalendarUnit | NSDayCalendarUnit; NSDateFormatter *format=[[NSDateFormatter alloc] init]; [format setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; NSDate *fromdate=[format dateFromString:dateStr]; NSTimeZone *fromzone = [NSTimeZone systemTimeZone]; NSInteger frominterval = [fromzone secondsFromGMTForDate: fromdate]; NSDate *fromDate = [fromdate dateByAddingTimeInterval: frominterval]; NSLog(@"fromdate=%@",fromDate); [format release]; NSDate *date = [NSDate date]; NSTimeZone *zone = [NSTimeZone systemTimeZone]; NSInteger interval = [zone secondsFromGMTForDate: date]; NSDate *localeDate = [date dateByAddingTimeInterval: interval]; NSLog(@"enddate=%@",localeDate); NSDateComponents *components = [gregorian components:unitFlags fromDate:fromDate toDate:localeDate options:0]; NSInteger months = [components month]; NSInteger days = [components day];//年[components year] NSLog(@"month=%d",months); NSLog(@"days=%d",days); [gregorian release]; if (months==0&&days;==0) { dateStr=[[dateStr substringFromIndex:11]substringToIndex:5]; cell.textLabel.text=[NSString stringWithFormat:@"今天 %@",dateStr];//今天 11:23 }else if(months==0&&days;==1){ dateStr=[[dateStr substringFromIndex:11]substringToIndex:5]; cell.textLabel.text=[NSString stringWithFormat:@"昨天 %@",dateStr];//昨天 11:23 }else{ dateStr=[dateStr substringToIndex:10]; cell.textLabel.text=dateStr; }
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。
上一篇: jquery构造from提交表单
下一篇:C#线程处理演示代码
最新资讯
热门推荐