粗略计算两个 NSDate之间相差几天

来源:互联网 发布:java api 1.7中文版 编辑:程序博客网 时间:2024/05/22 17:14

最近项目里要增加一个请假模块,其中有一个控件是根据时间范围计算自然日。。。。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
原创粉丝点击