做倒计时后台传的不是时间戳,是时间然后需要与系统时间对比得出时间戳进行倒计时显示
#pragma mark -- 倒计时
// 倒计时时间 NSString* timeStr = task.receiptTime; NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateStyle:NSDateFormatterMediumStyle]; [formatter setTimeStyle:NSDateFormatterShortStyle]; [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; // 设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制 NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"]; [formatter setTimeZone:timeZone]; NSDate* date = [formatter dateFromString:timeStr]; //倒计时时间 NSDate *datenow = [NSDate date];//系统现在时间 //NSString *nowtimeStr = [formatter stringFromDate:datenow];//----------将nsdate按formatter格式转成 //时间转时间戳的方法: NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[date timeIntervalSince1970]]; NSLog(@"timeSp倒计时时间戳:%@",timeSp); NSString *timeNow = [NSString stringWithFormat:@"%ld", (long)[datenow timeIntervalSince1970]]; NSLog(@"timeSp系统现在时间戳:%@",timeNow); double timeInterval = [timeNow doubleValue]-[timeSp doubleValue]; _countDownView.countDownTimeInterval = timeInterval;
NSString* timeStr = @"2019-02-21 16:46:02"; NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateStyle:NSDateFormatterMediumStyle]; [formatter setTimeStyle:NSDateFormatterShortStyle]; [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制 //设置时区,这个对于时间的处理有时很重要 //例如你在国内发布信息,用户在国外的另一个时区,你想让用户看到正确的发布时间就得注意时区设置,时间的换算. //例如你发布的时间为2010-01-26 17:40:50,那么在英国爱尔兰那边用户看到的时间应该是多少呢? //他们与我们有7个小时的时差,所以他们那还没到这个时间呢...那就是把未来的事做了1550738762 NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"]; [formatter setTimeZone:timeZone]; NSDate* date = [formatter dateFromString:timeStr]; //------------将字符串按formatter转成nsdate NSDate *datenow = [NSDate date];//现在时间,你可以输出来看下是什么格式 NSString *nowtimeStr = [formatter stringFromDate:datenow];//----------将nsdate按formatter格式转成 //时间转时间戳的方法: NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[date timeIntervalSince1970]]; NSLog(@"timeSp:%@",timeSp); //时间戳的值 //时间戳转时间的方法 NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:1296035591]; NSLog(@"1296035591 = %@",confromTimesp); NSString *confromTimespStr = [formatter stringFromDate:confromTimesp]; NSLog(@"confromTimespStr = %@",confromTimespStr); //时间戳转时间的方法: NSDateFormatter* formatter = [[NSDateFormatter alloc] init]; [formatter setDateStyle:NSDateFormatterMediumStyle]; [formatter setTimeStyle:NSDateFormatterShortStyle]; [formatter setDateFormat:@"yyyyMMddHHMMss"]; NSDate *date = [formatter dateFromString:@"1283376197"]; NSLog(@"date1:%@",date);
//当前时间转换成秒 NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制 [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; //现在时间,你可以输出来看下是什么格式 NSDate *datenow = [NSDate date]; //----------将nsdate按formatter格式转成nsstring NSString *currentTimeString = [formatter stringFromDate:datenow]; NSLog(@"currentTimeString = %@",currentTimeString); //----------将nsdate按formatter格式转成秒 NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[datenow timeIntervalSince1970]]; NSLog(@"timeSp = %@",timeSp); //时间秒转换成当前时间 NSDate *date = [NSDate dateWithTimeIntervalSince1970:1550738691]; NSTimeZone *zone = [NSTimeZone systemTimeZone]; NSInteger interval = [zone secondsFromGMTForDate: date]; NSDate *localeDate = [date dateByAddingTimeInterval: interval]; NSLog(@"enddate=%@",localeDate);