iOS利用时间戳获得两个字符串时间差,并闪动提示用户剩余时间
2015-05-22 10:27
281 查看
最近用到了时间戳,好久没有用过,所以有些生疏了,所以特将代码贴到此处,供遇到类似问题的人借鉴。
- (void)ExamPaperDict:(NSDictionary *)examPaperDict{ //设置答题剩余时间 NSString *subMitTime = [examPaperDict objectForKey:@"SUBMITTIME"]; NSString *str1 = [subMitTime substringFromIndex:11]; NSString *str2 = [str1 substringToIndex:8]; NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateStyle:NSDateFormatterMediumStyle]; [formatter setTimeStyle:NSDateFormatterShortStyle]; [formatter setDateFormat:@"HH:mm:ss"]; NSDate* date = [formatter dateFromString:str2]; //把时间转化成时间戳 int timeSp1 = [date timeIntervalSince1970]; self.timeSp1 = timeSp1; //创建定时器 NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(showTime) userInfo:nil repeats:YES]; [[NSRunLoop currentRunLoop]addTimer:timer forMode:NSRunLoopCommonModes]; [timer fire]; }
/* * 定时器开始,此方法每隔1秒执行一次,显示剩余考试时间 */ - (void)showTime{ //获取当前时间 NSDate *myDate = [NSDate date]; NSTimeZone *zone = [NSTimeZone systemTimeZone]; NSInteger interval = [zone secondsFromGMTForDate: myDate]; NSDate *localeDate = [myDate dateByAddingTimeInterval: interval]; NSString *myDataStr = [NSString stringWithFormat:@"%@",localeDate]; NSString *mystr1 = [myDataStr substringFromIndex:11]; NSString *mystr2 = [mystr1 substringToIndex:8]; NSDateFormatter *formatter1 = [[NSDateFormatter alloc] init]; [formatter1 setDateStyle:NSDateFormatterMediumStyle]; [formatter1 setTimeStyle:NSDateFormatterShortStyle]; [formatter1 setDateFormat:@"HH:mm:ss"]; NSDate* date1 = [formatter1 dateFromString:mystr2]; int timeSp2 = [date1 timeIntervalSince1970]; int sp = self.timeSp1 - timeSp2; if (sp >=0 ) { // 时间戳转时间的方法 NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:sp]; NSString *labStr = [NSString stringWithFormat:@"%@",confromTimesp]; NSString *labstr1 = [labStr substringFromIndex:11]; NSString *labstr2 = [labstr1 substringToIndex:8]; self.endTimeLable1.text =[NSString stringWithFormat:@"离交卷时间:%@",labstr2]; } }
相关文章推荐
- ios 计算两个时间之间的时间差
- java 两个时间字符串的时间差
- iOS计算两个时间的时间差
- iOS 计算两个时间的时间差
- iOS计算两个时间的时间差
- 【代码笔记】iOS-两个时间字符串的比较
- 两个字符串时间算时间差
- 利用字符串和列表将两个通讯录文本合并为一个文本
- iOS开发--利用Genstring命令来本地化字符串
- java计算两个时间的时间差
- iOS通过iTunes search检测版本更新,并提示用户更新!
- Asp.Net 利用TimeSpan类实现时间差计算 并返回所需字符串(类似于SNS)
- js中将两个日期字符串相减 获得天数
- IOS 将时间字符串转换为时间戳
- iOS通过iTunes search检测版本更新,并提示用户更新
- ios NSDate NSTring long 时间戳与字符串转换
- iOS开发---计算两个日期的时间差
- iOS 时间戳转换为字符串
- Java 得到当前系统时间,时间转字符串,字符串转时间,以及求时间差
- iOS积累--字符串转化为NSDate, 获取当前时区的系统时间