1. 开启定时器,1秒调用一次
- (void)spareTimeRun {
self.timer = [NSTimerscheduledTimerWithTimeInterval:1.0target:selfselector:@selector(timerFireMethod:) userInfo:nilrepeats:YES];
}
2. 时间格式转化,用目标时间跟当前时间取得差时间。
-(void)timerFireMethod:(NSTimer*)theTimer {
TuanItemModel *tempGroupProductDic = [self._tuanList objectAtIndex:self.tuanPageIndex];
NSString *string = tempGroupProductDic.endDate;
NSDateFormatter *dateformatter =[[[NSDateFormatteralloc]init]autorelease];//定义NSDateFormatter用来显示格式
[dateformatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];//设定格式
NSDate *todate=[dateformatter dateFromString:string];
NSCalendar *cal = [NSCalendarcurrentCalendar];//定义一个NSCalendar对象
NSDate *today = [NSDate date];//得到当前时间
//用来得到具体的时差
unsignedint unitFlags = NSYearCalendarUnit |NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit |NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *d = [cal components:unitFlags fromDate:today toDate:todate options:0];
self.spareTimeLabel.text = [NSStringstringWithFormat:@"剩余时间%d天%d时%d分%d秒", [d day],[d hour], [d minute], [d second]];
//if ([d month]>0) {
//self.spareTimeLabel.text = [NSString stringWithFormat:@"%d月%d日%d时%d分%d秒",[d month], [d day],[d hour], [d minute], [d second]];
//}else {
//self.spareTimeLabel.text = [NSString stringWithFormat:@"%d日%d时%d分%d秒", [d day],[d hour], [d minute], [d second]];
//}
}