将时间戳转化为秒数
[NSDate date].timeIntervalSince1970
默认时区的时间
NSDate *datenow = [NSDate date];
时间戳对应的时间
NSTimeZone *zone = [NSTimeZonesystemTimeZone];
NSDate *jifen = [datenow dateByAddingTimeInterval:1430807556];
现在的时间
NSInteger interval = [zone secondsFromGMTForDate:datenow];
NSDate *localeDate = [datenow dateByAddingTimeInterval: interval];
NSString *timeSp = [NSStringstringWithFormat:@"%ld", (long)[localeDatetimeIntervalSince1970]];
***********************************************
在tabelView中显示倒计时
//每次更新数据的时候要重新更新这个数组
//剩余时间得数组
_arr = [NSArray arrayWithObjects:[NSNumber numberWithInt:100], [NSNumber numberWithInt:200], [NSNumber numberWithInt:300], nil];
for (NSInteger i = 0; i < _arr.count; i++) {
NSDictionary *dic =@{@"row":[NSStringstringWithFormat:@"%i", i],@"lastTime":_arr[i]};
[totalLastTime addObject:dic];
}
//开启定时器方法:在viewDidLoad里调用 startTimer
- (void)startTimer
{
//每隔一分钟刷新
timer = [NSTimerscheduledTimerWithTimeInterval:1target:selfselector:@selector(refreshLessTime)userInfo:@""repeats:YES];
//如果不添加下面这条语句,在UITableView拖动的时候,会阻塞定时器的调用
[[NSRunLoop currentRunLoop]addTimer:timerforMode:UITrackingRunLoopMode];
}
//主要的定时器中的方法,在该方法中,遍历totalLastTime,取出其中保存的lasttime和row,time用来显示,在显示完后自减,row代表对应显示的位置,在一次循环后,将新的time和没有改变的row从新替换totalLastTime中对应位置的元素,以此保证每一秒执行时,显示time都是最新的。
- (void)refreshLessTime
{
NSInteger time;
for (int i = 0; i < totalLastTime.count; i++) {
time = [[[totalLastTimeobjectAtIndex:i] objectForKey:@"lastTime"]integerValue];
NSIndexPath *indexPath = [NSIndexPathindexPathForItem:[[[totalLastTimeobjectAtIndex:i] objectForKey:@"row"]integerValue] inSection:0];
TableViewCell *cell = (TableViewCell *)[_tableViewcellForRowAtIndexPath:indexPath];
cell.label.text = [NSStringstringWithFormat:@"剩余支付时间:%@",[selflessSecondToDay:time]];
NSDictionary *dic =@{@"row": [NSStringstringWithFormat:@"%i",indexPath.row],@"lastTime": [NSNumbernumberWithInteger:(time - 1)]};
[totalLastTimereplaceObjectAtIndex:i withObject:dic];
}
[_tableView reloadData];
}
部分参考http://m.blog.csdn.net/blog/lidechen/43191779