objective-c字符串转化成NSDate类型,计算与当前时间的相差,月数,天数

objective-c字符串转化成NSDate类型,计算与当前时间的相差,月数,天数,第1张

概述objective-c字符串转化成NSDate类型,计算与当前时间的相差,月数,天数

下面是内存溢出 jb51.cc 通过网络收集整理的代码片段。

内存溢出小编现在分享给大家,也给大家做个参考。

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;    }

以上是内存溢出(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

总结

以上是内存溢出为你收集整理的objective-c字符串转化成NSDate类型,计算与当前时间的相差,月数,天数全部内容,希望文章能够帮你解决objective-c字符串转化成NSDate类型,计算与当前时间的相差,月数,天数所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1106158.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存