ios开发之--把秒转换为天时分秒

时间:2023-02-12 12:42:58

把秒转换成时分秒:

- (NSString *)timeFormatted:(int)totalSeconds
{ int seconds = totalSeconds % ;
int minutes = (totalSeconds / ) % ;
int hours = totalSeconds / ; return [NSString stringWithFormat:@"%02d:%02d:%02d",hours, minutes, seconds];
}

把毫秒转换成时间格式:

    //将时间数据(毫秒)转换为天和小时
- (NSString*)getOvertime:(NSString*)mStr
{
long msec = [mStr longLongValue]; if (msec <= )
{
return @"";
} NSInteger d = msec////;
NSInteger h = msec///%;
//NSInteger m = msec/1000/60%60;
//NSInteger s = msec/1000%60; NSString *_tStr = @"";
NSString *_dStr = @"";
NSString *_hStr = @"";
NSString *_hTimeType = @"defaultColor"; if (d > )
{
_dStr = [NSString stringWithFormat:@"%ld天",d];
} if (h > )
{
_hStr = [NSString stringWithFormat:@"%ld小时",h];
} //小于2小时 高亮显示
if (h > && h < )
{
_hTimeType = @"hightColor";
} _tStr = [NSString stringWithFormat:@"%@%@后到期-%@",_dStr,_hStr,_hTimeType]; return _tStr;
}