(iOS开发)计算时间差
来源:互联网 发布:wps输入数据自动计算 编辑:程序博客网 时间:2024/04/30 17:59
方法1:(限定:IOS8.0之后使用)
//计算时间差-(int)calculateTime{ int timeResult=0; NSDateFormatter *date=[[NSDateFormatter alloc] init]; [date setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; NSString *messageTime=[[NSUserDefaults standardUserDefaults] objectForKey:@"lastTime"]; if (messageTime.length>0) { NSDate *messageDate=[date dateFromString:messageTime]; NSCalendar *cal = [NSCalendar currentCalendar]; NSString *timeNow=[self getTimeNow]; NSDate *nowDate=[date dateFromString:timeNow]; unsigned int unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit; NSDateComponents *d = [cal components:unitFlags fromDate:messageDate toDate:nowDate options:0]; timeResult = (int)[d hour]*3600+(int)[d minute]*60+(int)[d second]+(int)[d day]*86400+(int)[d month]*2592000+(int)[d year]*31536000; NSLog(@"timeResult: %d",timeResult); if (timeResult<0) { timeResult=0; } } return timeResult;}
-(NSString *)getTimeNow{ NSDate *GMTDate = [NSDate date]; NSTimeZone *timeZone = [NSTimeZone systemTimeZone]; NSInteger interval = [timeZone secondsFromGMT]; NSDate *localeDate = [GMTDate dateByAddingTimeInterval:interval]; NSString *time=[NSString stringWithFormat:@"%@",localeDate]; NSString *timenow=[[NSString alloc]init]; timenow=[time substringToIndex:19]; return timenow;}
方法2:
//后来时间 NSDate *currentDate = [NSDate date];//获取当前时间,日期 NSInteger nowtime=[currentDate timeIntervalSince1970]; //间隔时间 NSInteger intervalSinceNow =[self distanceTimeWithBeforeTime:begintime withNowDateTime:nowtime]; [submitLog saveLogDatadxID:_releationid timeValue:intervalSinceNow AssetsName:_AssetsName bookID:nil bookName:nil subjectID:_subjectID beginTime:beginDateString];//初始时间 NSDate *currentDate = [NSDate date];//获取当前时间,日期 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; beginDateString = [dateFormatter stringFromDate:currentDate]; begintime =[currentDate timeIntervalSince1970];//计算方法 - (NSInteger)distanceTimeWithBeforeTime:(NSInteger)beTime withNowDateTime:(NSInteger)nowTime{ NSInteger distanceTime = nowTime - beTime; return distanceTime;}
0 0
- (iOS开发)计算时间差
- iOS 计算时间差(笔记)
- iOS开发---计算两个日期的时间差
- ios计算时间差
- iOS 计算时间差
- IOS 计算时间差
- IOS 计算时间差
- iOS计算时间差
- IOS根据date计算时间差
- iOS计算时间差-倒计时时间
- 【代码笔记】iOS-计算时间差
- iOS 获取当前时间以及计算年龄(时间差)
- iOS 获取当前时间以及计算年龄(时间差)
- Ios开发 Nsdate和字符串之间的转换,时间差计算总结
- 关于ios中时间差的计算
- iOS 计算两个时间的时间差
- IOS 计算两个时区之间的时间差
- ios 计算两个时间之间的时间差
- 怎么去理解Spring Aop
- 用git与github建立连接
- 百度地图--路线(步行)
- C语言----链表
- android 控件 recyclerview
- (iOS开发)计算时间差
- mybatis学习(3)---加载配置文件
- Android中解决复写ListView时出现OnScollListener无法监听,这时如果实现标题栏渐变色的话,应该将计算滚动距离的方法,写在不断调用的回调中。
- 洛谷 P1372 又是毕业季I
- android View长按事件简单写法
- 近期开发中用到的一些东西(关于自适应大小,字符串处理,心跳,本地推送,json处理等)
- 代码中特殊的注释技术——TODO、FIXME和XXX的用处
- swiper在IE8下失效
- swift--Swift 3.0实战:和Objective-C的交互需要注意