NSTimeInterval的使用

本文深入探讨了iOS开发的关键技术和实践经验,包括Objective-C和Swift语言的高级特性、Xcode开发环境的最佳实践、移动应用性能优化策略,以及如何利用Cocoa Touch和UIKit构建高质量的iOS应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第一种方式:
NSTimeInterval time = ...;
NSString *string = [NSString stringWithFormat:@"%02li:%02li:%02li",
lround(floor(time / 3600.)) % 100,
lround(floor(time / 60.)) % 60,
lround(floor(time.)) % 60];

第二种方式:
// You could also have stored the start time using
// CFAbsoluteTimeGetCurrent()
NSTimeInterval elapsedTime = [startDate timeIntervalSinceNow];

// Divide the interval by 3600 and keep the quotient and remainder
div_t h = div(elapsedTime, 3600);
int hours = h.quot;
// Divide the remainder by 60; the quotient is minutes, the remainder
// is seconds.
div_t m = div(h.rem, 60);
int minutes = m.quot;
int seconds = m.rem;

// If you want to get the individual digits of the units, use div again
// with a divisor of 10.

NSLog(@"%d:%d:%d", hours, minutes, seconds)

如果您有您初始日期存储在 NSDate 对象时,您可以获得新日期任何时间间隔在未来。只需使用 dateByAddingTimeInterval: 像这样:

NSDate * originalDate = [NSDate date];
NSTimeInterval interval = 1;
NSDate * futureDate = [originalDate dateByAddingTimeInterval:interval];

ios获取自1970年以来的毫秒数同java的System.currentTimeMillis()

+ (NSString*)generateTimeIntervalWithTimeZone
{
NSMutableString *result = [[NSMutableString alloc] init];
NSDate *date = [NSDate date];
NSTimeInterval time = [date timeIntervalSince1970]*1000;//毫秒数要乘以1000
double i=time; //NSTimeInterval返回的是double类型


NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:@"Z"];
NSString *timeZone = [format stringFromDate:date];
NSString *timeIntervalStr = [NSString stringWithFormat:@"%.f", i];

[result appendString:timeIntervalStr];
[result appendString:timeZone];

return result;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值