读书人

NSTimeInterval的施用

发布时间: 2012-06-27 14:20:08 作者: rapoo

NSTimeInterval的使用
第一种方式:
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];

读书人网 >移动开发

热点推荐