计算有效期字符串转NSDate 转字符串

项目的需求是根据后台返回给的有效期来结合当天时间计算出会员的剩余天数

//首先要处理后台给的字符串 转换成自己想要的格式
NSArray *array = [[NSString stringWithFormat:@"%@", @"2020-03-17T00:00:00Z"] componentsSeparatedByString:@"T"];
NSDateFormatter*df = [[NSDateFormatter alloc]init];//格式化
[df setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
[df setLocale:[[NSLocale alloc]initWithLocaleIdentifier:@"zh_CN"] ];
        
NSDate*date =[[NSDate alloc]init];
        
NSString *string = [[NSString stringWithFormat:@"%@", array[0]] stringByAppendingString:[[NSString stringWithFormat:@" %@", array[1]] substringWithRange:NSMakeRange(0, [NSString stringWithFormat:@"%@", array[1]].length - 1)]];
        
date =[df dateFromString:[NSString stringWithFormat:@"%@", string]];
//转换成秒
NSTimeInterval timeStamp= [date timeIntervalSince1970];
        
NSLog(@"%@", array[0]);
[11:43:06] -[PersonalTrainingMembersBaseTableViewCell setModel:] [第191行] 2017-03-31

NSLog(@"%@", string);
[11:43:06] -[PersonalTrainingMembersBaseTableViewCell setModel:] [第192行] 2017-03-31 00:00:0

NSLog(@"%f", timeStamp);
[11:43:06] -[PersonalTrainingMembersBaseTableViewCell setModel:] [第193行] 1490889600.000000      


//获取当前日期
NSDate *dateToday = [NSDate date];
        
NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init];
        
[dateformatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
        
NSString *timeTodayStr = [dateformatter stringFromDate:dateToday];
        
NSTimeInterval timeStamp1= [dateToday timeIntervalSince1970];
        
NSLog(@"%@", timeTodayStr);
NSLog(@"%f", timeStamp1);
        
int time =   (timeStamp -timeStamp1) / 86400;
        
self.validityTimeLabel.text = [NSString stringWithFormat:@"会籍有效天数(天):%d", time];

你可能感兴趣的:(计算有效期字符串转NSDate 转字符串)