1. 程式人生 > >IOS 秒數轉換為天數和小時數

IOS 秒數轉換為天數和小時數

獲取時間字串,然後根據“-”將其分開,陣列一是時間值,陣列二是時間值的顏色。
NSString *tempStr = [self getOvertime:time];
        NSArray *tempArray = [tempStr componentsSeparatedByString:@"-"];
        
        _dataStr =  [tempArray objectAtIndex:0];
        
        NSString *_colorType = [tempArray objectAtIndex:1];

//將時間資料(毫秒)轉換為天和小時
- (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;
}