• (NSString *)convertSecondsToDHM:(NSTimeInterval)seconds { // 计算天数 NSInteger days = seconds / (60 * 60 * 24);

    // 计算剩余小时数 seconds -= days * (60 * 60 * 24); NSInteger hours = seconds / (60 * 60);

    // 计算剩余分钟数 seconds -= hours * (60 * 60); NSInteger minutes = seconds / 60;

    // 构建转换结果字符串 NSMutableString *result = [NSMutableString string]; if (days > 0) { [result appendFormat:'%ld天 ', (long)days]; } if (hours > 0) { [result appendFormat:'%ld小时 ', (long)hours]; } if (minutes > 0) { [result appendFormat:'%ld分钟', (long)minutes]; }

    return [result copy]; }

使用示例:

NSTimeInterval seconds = 123456;
NSString *convertedTime = [self convertSecondsToDHM:seconds];
NSLog(@'%@', convertedTime);

输出结果:

1天 10小时 17分钟
iOS 开发:将时间秒数转换为天、小时、分钟 (OC 代码示例)

原文地址: http://www.cveoy.top/t/topic/qeH9 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录