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

把秒转换成时分秒:

- (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;  
    }  

 

posted @ 2017-11-16 15:31  稻草人11223  阅读(6078)  评论(0编辑  收藏  举报
返回顶部