您的位置:首页 > 其它

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

2014-02-08 09:48 363 查看
- (NSString *)intervalSinceNow: (NSString *) theDate
{
NSString *timeString=@"";

NSDateFormatter *format=[[NSDateFormatter alloc] init];
[format setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *fromdate=[format dateFromString:theDate];
NSTimeZone *fromzone = [NSTimeZone systemTimeZone];
NSInteger frominterval = [fromzone secondsFromGMTForDate: fromdate];
NSDate *fromDate = [fromdate  dateByAddingTimeInterval: frominterval];

//获取当前时间
NSDate *adate = [NSDate date];
NSTimeZone *zone = [NSTimeZone systemTimeZone];
NSInteger interval = [zone secondsFromGMTForDate: adate];
NSDate *localeDate = [adate  dateByAddingTimeInterval: interval];

double intervalTime = [fromDate timeIntervalSinceReferenceDate] - [localeDate timeIntervalSinceReferenceDate];
long lTime = fabs((long)intervalTime);
NSInteger iSeconds =  lTime % 60;
NSInteger iMinutes = (lTime / 60) % 60;
NSInteger iHours = fabs(lTime/3600);
NSInteger iDays = lTime/60/60/24;
NSInteger iMonth =lTime/60/60/24/12;
NSInteger iYears = lTime/60/60/24/384;

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

if (iHours<1 && iMinutes>0)
{
timeString=[NSString stringWithFormat:@"%d分",iMinutes];

}else if (iHours>0&&iDays<1 && iMinutes>0) {
timeString=[NSString stringWithFormat:@"%d时%d分",iHours,iMinutes];
}
else if (iHours>0&&iDays<1) {
timeString=[NSString stringWithFormat:@"%d时",iHours];
}else if (iDays>0 && iHours>0)
{
timeString=[NSString stringWithFormat:@"%d天%d时",iDays,iHours];
}
else if (iDays>0)
{
timeString=[NSString stringWithFormat:@"%d天",iDays];
}
return timeString;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐