iOS 秒数转换成时间,时,分,秒

时间:2024-08-06 21:07:32

//转换成时分秒

- (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 *)timeFormatted:(int)totalSeconds

{
   NSDate  *date = [NSDate dateWithTimeIntervalSince1970:totalSeconds];
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSInteger interval = [zone secondsFromGMTForDate: date];
    NSDate *localeDate = [date  dateByAddingTimeInterval: interval];
    NSLog(@"enddate=%@",localeDate);
}