秒数换算成“xx天xx小时xx分xx秒”格式算法
jopen
9年前
-
懒加载NSDateFormatter
-(NSDateFormatter *)fmt{ if (_fmt==nil) { _fmt = [[NSDateFormatter alloc] init]; _fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss"; _fmt.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]; } return _fmt; }
2.获取两个时间之间的差距
#pragma mark - 获取截止时间 /** * 比较两个时间之间的差 */ -(long long)compareStartTime:(NSString *)startTime endTime:(NSString *)endTime { //模拟数据 startTime = @"2016-12-20 23:59:59"; endTime = @"2016-12-11 23:59:59"; NSDate *startDate = [self.fmt dateFromString:startTime]; NSDate *endDate = [self.fmt dateFromString:endTime]; long long seconds = (long long)[startDate timeIntervalSinceDate:endDate]; return seconds; }
3.获取时间差字符串
/** * 获取时间差字符串(xx天xx小时xx分xx秒) */ -(NSString *)timeformatFromSeconds:(long long)seconds{ //format of day NSString *str_day = [NSString stringWithFormat:@"%02lld",seconds/(86400)]; //format of hour NSString *str_hour = [NSString stringWithFormat:@"%0lld",(seconds%86400)/3600]; //format of minute NSString *str_minute = [NSString stringWithFormat:@"%02lld",(seconds%3600)/60]; //format of second NSString *str_second = [NSString stringWithFormat:@"%02lld",seconds%60]; //format of time NSString *format_time = [NSString stringWithFormat:@"%@天%@小时%@分%@秒",str_day,str_hour,str_minute,str_second]; return format_time; }