计算两个日期之间相差几天几小时几分钟
原创
©著作权归作者所有:来自51CTO博客作者mb62cf8bca18558的原创作品,请联系作者获取转载授权,否则将追究法律责任
NSDateFormatter
*dateFormatter=[[
NSDateFormatter
alloc
]
init
];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate * senddate=[NSDate date];
//结束时间
NSDate *endDate = [dateFormatter dateFromString:@"2014-6-24 00:00:00"];
//当前时间
NSDate *senderDate = [dateFormatter dateFromString:[dateFormatter stringFromDate:senddate]];
//得到相差秒数
NSTimeInterval time=[endDate timeIntervalSinceDate:senderDate];
int days = ((int)time)/(3600*24);
int hours = ((int)time)%(3600*24)/3600;
int minute = ((int)time)%(3600*24)600/60;
if (days <= 0&&hours; <= 0&&minute; <= 0)
dateContent=@"0天0小时0分钟";
else
dateContent=[[NSString alloc] initWithFormat:@"%i天%i小时%i分钟",days,hours,minute];