NSDateFormatter *dateFormater = [[NSDateFormatter alloc] init];
[dateFormater setDateFormat:@"yyyyMMddHHmmss"];
NSString *nowtime = [NSString stringWithFormat:@"%@",[dateFormater stringFromDate:now]];
_timer= [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(daoJi:) userInfo:_keyTime repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
_daojishi = [nowtime integerValue]-[_keyTime integerValue];
- (NSString*)getTime{
if (!_keyTime) {
return @"暂无活动";
}
id obj = _keyTime;
NSString *stayears = [obj substringToIndex:4];
NSString *stanowmouths = [obj substringFromIndex:4 toIndex:6];
NSString *stanowday = [obj substringFromIndex:6 toIndex:8];
NSString *stanowhour = [obj substringFromIndex:8 toIndex:10];
NSString *stanowminute = [obj substringFromIndex:10 toIndex:12];
NSString *stanowsecond = [obj substringFromIndex:12 toIndex:14];
NSDateFormatter *f1 = [[NSDateFormatter alloc] init];
[f1 setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *theDay = [f1 dateFromString:[NSString stringWithFormat:@"%@-%@-%@ %@:%@:%@",stayears,stanowmouths,stanowday,stanowhour,stanowminute,stanowsecond]];
NSCalendar *cal = [NSCalendar currentCalendar];//定义一个NSCalendar对象
NSDate *today = [NSDate date];//得到当前时间
//用来得到具体的时差
unsigned int unitFlags = NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *d = [cal components:unitFlags fromDate:today toDate:theDay options:0];
if ([d minute]<10 && [d hour]<10 &&[d minute] >0 && [d hour]>0) {
NSString *countdown = [NSString stringWithFormat:@"0%ld:0%ld:%ld", (long)[d hour], (long)[d minute], (long)[d second]];
return countdown;
}
else if([d hour]<10 && [d minute]>10){
NSString *countdown = [NSString stringWithFormat:@"0%ld:%ld:%ld", (long)[d hour], (long)[d minute], (long)[d second]];
return countdown;
}
else if([d minute]<10 && [d hour]>10){
NSString *countdown = [NSString stringWithFormat:@"%ld:0%ld:%ld", (long)[d hour], (long)[d minute], (long)[d second]];
return countdown;
}
else if([d hour] <=0 && [d minute] <= 0 && [d second] <= 0) {
[_timer invalidate];
return @"本次活动已经开始,赶快抢购吧~";
}
else{
NSString *countdown = [NSString stringWithFormat:@"%ld:%ld:%ld", (long)[d hour], (long)[d minute], (long)[d second]];
return countdown;
}
}
- (void)daoJi:(NSTimer*)aTimer
{
_remainLa.text = [self getTime];
return ;
}