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

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

}

你可能感兴趣的:(计算某一天距离今天多少天,多少小时,多少分钟)