最近项目里要增加一个请假模块,其中有一个控件是根据时间范围计算自然日。。。。blabla,话不投机半句多,上代码。
+ (NSInteger)calcDaysFromBegin:(NSDate *)beginDate end:(NSDate *)endDate
{
// 创建日期格式化对象
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
// 取两个日期对象的时间间隔
// 这里的 NSTimeInterval 并不是对象,是基本型,其实是double 类型,是由 C 定义的: typedef double NSTimeInterval;
NSTimeInterval time = [endDate timeIntervalSinceDate:beginDate];
int days = ((int)time)/(3600 * 24);
return days;
}
大功告成,end