计算两个日期相差多少秒

//得到当前日期
    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);

posted @ 2012-04-12 20:49  周宏伟  阅读(1208)  评论(0编辑  收藏  举报