- //////每隔一秒执行一次
- NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerFire:) userInfo:nil repeats:YES];
- - (void)timerFire:(NSTimer *)timer
- {
- NSCalendar *calendar = [NSCalendar currentCalendar];
- NSDateComponents *components = [[NSDateComponents alloc] init];
- [components setYear:2014];
- [components setMonth:12];
- [components setDay:12];
- [components setHour:12];
- [components setMinute:0];
- [components setSecond:0];
- NSDate *fireDate = [calendar dateFromComponents:components];//目标时间
- NSDate *today = [NSDate date];//当前时间
- unsigned int unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
- NSDateComponents *d = [calendar components:unitFlags fromDate:today toDate:fireDate options:0];//计算时间差
- auctionTime.text = [NSString stringWithFormat:@"%d天%d小时%d%分%d秒", [d day], [d hour], [d minute], [d second]];//倒计时显示
- }
复制代码 |