(iOS开发)计算时间差
2016-09-20 15:31
447 查看
方法1:(限定:IOS8.0之后使用)
-(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:
//计算时间差 -(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; }
相关文章推荐
- ios开发之 时间日期差计算
- iOS开发计算距离现在时间
- iOS开发 时间戳时间计算
- iOS开发——计算时间差,计算日期差,计算年龄。
- UIDatePicker 日期/时间选取器(滚轮)—IOS开发
- ios计算时间差 转
- ios 计算方法执行的时间
- 关于ios中时间差的计算
- UIDatePicker 日期/时间选取器(滚轮)—IOS开发
- IOS_计算当前时间与某时间的时间差
- ios开发教程之申请更多后台时间
- UIDatePicker 日期/时间选取器(滚轮)—IOS开发
- iOS 计算某个时间到现在是多少月/天/时的代码
- ios开发之获取系统开机时间
- ios 计算方法执行的时间
- [ios]字符串转化成NSDate类型 计算与当前时间的相差 月数 天数 【转】
- iphone开发计算时间间隔
- ios 开发中获得当前时间的 年月日,时分秒
- IOS开发基础教程学习笔记2 DatePicker获取时间
- ios开发 获取系统时间