秒數換算成“xx天xx小時xx分xx秒”格式算法
-
懶加載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; }
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!