把秒轉換成時分秒:
- (NSString *)timeFormatted:(int)totalSeconds { int seconds = totalSeconds % 60; int minutes = (totalSeconds / 60) % 60; int hours = totalSeconds / 3600; return [NSString stringWithFormat:@"%02d:%02d:%02d",hours, minutes, seconds]; }
把毫秒轉換成時間格式:
//將時間數據(毫秒)轉換為天和小時 - (NSString*)getOvertime:(NSString*)mStr { long msec = [mStr longLongValue]; if (msec <= 0) { return @""; } NSInteger d = msec/1000/60/60/24; NSInteger h = msec/1000/60/60%24; //NSInteger m = msec/1000/60%60; //NSInteger s = msec/1000%60; NSString *_tStr = @""; NSString *_dStr = @""; NSString *_hStr = @""; NSString *_hTimeType = @"defaultColor"; if (d > 0) { _dStr = [NSString stringWithFormat:@"%ld天",d]; } if (h > 0) { _hStr = [NSString stringWithFormat:@"%ld小時",h]; } //小於2小時 高亮顯示 if (h > 0 && h < 2) { _hTimeType = @"hightColor"; } _tStr = [NSString stringWithFormat:@"%@%@后到期-%@",_dStr,_hStr,_hTimeType]; return _tStr; }