您的位置:首页 > 其它

计算两个时间相距离多少

2012-04-14 09:20 183 查看
//得到当前日期

CFAbsoluteTime currTime=CFAbsoluteTimeGetCurrent();

CFGregorianDate currenttDate=CFAbsoluteTimeGetGregorianDate(currTime,CFTimeZoneCopyDefault());

//得到要提醒的日期

CFGregorianDate clockDate=CFAbsoluteTimeGetGregorianDate(currTime, CFTimeZoneCopyDefault());

clockDate.hour=5;

clockDate.minute=10;

clockDate.second=1;

NSLog(@"currdata,year=%ld",currenttDate.year);

//把两个日期存放到tm类型的变量中

struct tm t0,t1;

t0.tm_year=currenttDate.year - 1900;

t0.tm_mon=currenttDate.month;

t0.tm_mday=currenttDate.day;

t0.tm_hour=currenttDate.hour;

t0.tm_min=currenttDate.minute;

t0.tm_sec=currenttDate.second;

t1.tm_year=clockDate.year - 1900;

t1.tm_mon=clockDate.month;

t1.tm_mday=clockDate.day;

t1.tm_hour=clockDate.hour;

t1.tm_min=clockDate.minute;

t1.tm_sec=clockDate.second;

NSLog(@"%d",t1.tm_yday);

NSLog(@"time1=%ld ,time2=%ld",mktime(&t1),mktime(&t0));

NSTimeInterval time=mktime(&t1)-mktime(&t0);

if (time<0) {

t1.tm_wday++;

time=mktime(&t1)-mktime(&t0);

}

NSLog(@"相差%f秒",time);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: