使用 NSDate,NSCalendar, NSDateComponents 获得时间之差总结
2014-06-26 10:39
309 查看
方法一
NSDate* toDate1 = [ [ NSDate alloc] initWithString:@"2520-9-26 17:10:00 +0600" ];
NSTimeInterval distance = [ toDate1 timeIntervalSinceNow ];
NSTimeInterval iDat = distance / ( 86400 ) ;
NSLog( @" From now to %@ diff: %f ", [toDate1 description ], iDat );
[ toDate1 release ];
方法二
NSDate* toDate = [ [ NSDate alloc] initWithString:@"2009-9-29 0:0:00 +0600" ];
NSDate* startDate = [ [ NSDate alloc] init ];
NSCalendar* chineseClendar = [ [ NSCalendar alloc ] initWithCalendarIdentifier:NSGregorianCalendar ];
NSUInteger unitFlags = NSHourCalendarUnit | NSMinuteCalendarUnit |
NSSecondCalendarUnit | NSDayCalendarUnit
| NSMonthCalendarUnit | NSYearCalendarUnit;
NSDateComponents *cps = [ chineseClendar components:unitFlags fromDate:startDate toDate:toDate options:0];
NSInteger diffHour = [ cps hour ];
NSInteger diffMin = [ cps minute ];
NSInteger diffSec = [ cps second ];
NSInteger diffDay = [ cps day ];
NSInteger diffMon = [ cps month ];
NSInteger diffYear = [ cps year ];
NSLog( @" From Now to %@, diff: Years: %d Months: %d, Days; %d, Hours: %d, Mins:%d, sec:%d",
[toDate description], diffYear, diffMon, diffDay, diffHour, diffMin,diffSec );
[ toDate release ];
[ startDate release ];
[ chineseClendar release ];
PS:获取年月日的方法
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:[NSDate date]];
NSInteger day = [components day];
NSInteger month= [components month];
NSInteger year= [components year];
NSDate* toDate1 = [ [ NSDate alloc] initWithString:@"2520-9-26 17:10:00 +0600" ];
NSTimeInterval distance = [ toDate1 timeIntervalSinceNow ];
NSTimeInterval iDat = distance / ( 86400 ) ;
NSLog( @" From now to %@ diff: %f ", [toDate1 description ], iDat );
[ toDate1 release ];
方法二
NSDate* toDate = [ [ NSDate alloc] initWithString:@"2009-9-29 0:0:00 +0600" ];
NSDate* startDate = [ [ NSDate alloc] init ];
NSCalendar* chineseClendar = [ [ NSCalendar alloc ] initWithCalendarIdentifier:NSGregorianCalendar ];
NSUInteger unitFlags = NSHourCalendarUnit | NSMinuteCalendarUnit |
NSSecondCalendarUnit | NSDayCalendarUnit
| NSMonthCalendarUnit | NSYearCalendarUnit;
NSDateComponents *cps = [ chineseClendar components:unitFlags fromDate:startDate toDate:toDate options:0];
NSInteger diffHour = [ cps hour ];
NSInteger diffMin = [ cps minute ];
NSInteger diffSec = [ cps second ];
NSInteger diffDay = [ cps day ];
NSInteger diffMon = [ cps month ];
NSInteger diffYear = [ cps year ];
NSLog( @" From Now to %@, diff: Years: %d Months: %d, Days; %d, Hours: %d, Mins:%d, sec:%d",
[toDate description], diffYear, diffMon, diffDay, diffHour, diffMin,diffSec );
[ toDate release ];
[ startDate release ];
[ chineseClendar release ];
PS:获取年月日的方法
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:[NSDate date]];
NSInteger day = [components day];
NSInteger month= [components month];
NSInteger year= [components year];
相关文章推荐
- 使用 NSDate,NSCalendar, NSDateComponents 获得时间之差总结
- 使用 NSDate,NSCalendar, NSDateComponents 获得时间之差总结 .
- 使用 NSDate,NSCalendar, NSDateComponents 获得时间之差总结
- NSDate,NSCalendar, NSDateComponents 获得时间之差总结
- iOS--对iOS时间类的代码总结 NSCalendar NSDateComponents NSDate
- iOS--对iOS时间类的代码总结 NSCalendar NSDateComponents NSDate
- iOS开发中获取上月的起止时间(NSCalendar、NSDateComponents和NSDate的使用)
- ios正在使用NSDateComponents、NSDate、NSCalendar它的结论是在当前时间是在一段时间在一天。
- MySQL日期数据类型、时间类型使用总结
- 关于SQL SERVER时间格式使用的一些总结
- Java日期时间使用总结
- VC下写入txt的一点总结&&如何获得当前应用程序的目录&&获得系统日期时间
- MySQL:MySQL日期数据类型、MySQL时间类型使用总结
- SQL SERVER特殊时间格式使用总结
- Java日期时间使用总结
- windows 使用GetLocalTime()和GetSystemTime()所获得的时间不同
- Sql常见问题总结二(Sql语句怎么样查询IP,游标去重复,各种函数使用,各种取时间格式,字符串精确排序,超时锁问题)
- 关于"优先使用时间上较早获得的积分"的实现思路
- 关于SQL SERVER时间格式使用的一些总结 综合整理
- 使用javascript获得URL及参数,停留时间