首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

NSTimeInterval的施用

2012-06-27 
NSTimeInterval的使用第一种方式:NSTimeInterval time ...NSString *string [NSString stringWithFor

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];

热点排行