天天看点

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

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

NSTimeInterval的使用

+ (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;  

}  

上一篇: 详解NStimer