美文网首页
计算某一天距离今天多少天,多少小时,多少分钟

计算某一天距离今天多少天,多少小时,多少分钟

作者: Bonucci | 来源:发表于2017-03-01 15:03 被阅读0次

    - (NSString*)intervalSinceNow: (NSString*) theDate

    {

    NSString*timeString=@"";

    NSDateFormatter*format=[[NSDateFormatteralloc] init];

    [format setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

    NSDate*fromdate=[format dateFromString:theDate];

    NSTimeZone*fromzone = [NSTimeZonesystemTimeZone];

    NSIntegerfrominterval = [fromzone secondsFromGMTForDate: fromdate];

    NSDate*fromDate = [fromdate  dateByAddingTimeInterval: frominterval];

    //获取当前时间

    NSDate*adate = [NSDatedate];

    NSTimeZone*zone = [NSTimeZonesystemTimeZone];

    NSIntegerinterval = [zone secondsFromGMTForDate: adate];

    NSDate*localeDate = [adate  dateByAddingTimeInterval: interval];

    doubleintervalTime = [fromDate timeIntervalSinceReferenceDate] - [localeDate timeIntervalSinceReferenceDate];

    longlTime = fabs((long)intervalTime);

    NSIntegeriSeconds =  lTime % 60;

    NSIntegeriMinutes = (lTime / 60) % 60;

    NSIntegeriHours = fabs(lTime/3600);

    NSIntegeriDays = lTime/60/60/24;

    NSIntegeriMonth =lTime/60/60/24/12;

    NSIntegeriYears = lTime/60/60/24/384;

    NSLog(@"相差%d年%d月 或者 %d日%d时%d分%d秒", iYears,iMonth,iDays,iHours,iMinutes,iSeconds);

    if(iHours<1 && iMinutes>0)

    {

    timeString=[NSStringstringWithFormat:@"%d分",iMinutes];

    }elseif(iHours>0&&iDays<1 && iMinutes>0) {

    timeString=[NSStringstringWithFormat:@"%d时%d分",iHours,iMinutes];

    }

    elseif(iHours>0&&iDays<1) {

    timeString=[NSStringstringWithFormat:@"%d时",iHours];

    }elseif(iDays>0 && iHours>0)

    {

    timeString=[NSStringstringWithFormat:@"%d天%d时",iDays,iHours];

    }

    elseif(iDays>0)

    {

    timeString=[NSStringstringWithFormat:@"%d天",iDays];

    }

    returntimeString;

    }

    相关文章

      网友评论

          本文标题:计算某一天距离今天多少天,多少小时,多少分钟

          本文链接:https://www.haomeiwen.com/subject/snaqgttx.html