iOS 计算日期间隔天数

释放双眼,带上耳机,听听看~!

计算两个日期间隔天数,下面贴上代码,计算指定日期与当前日期间隔天数:

- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
NSInteger *days = [self computeDaysWithDataFromString:@"2016-04-24"];
NSLog(@"days=%ld", days);
}
//计算日期间隔天数
- (NSInteger)computeDaysWithDataFromString:(NSString *)string
{
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[gregorian setFirstWeekday:2];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSDate *fromDate;
NSDate *toDate;
[gregorian rangeOfUnit:NSDayCalendarUnit startDate:&fromDate interval:NULL forDate:[dateFormatter dateFromString:string]];
[gregorian rangeOfUnit:NSDayCalendarUnit startDate:&toDate interval:NULL forDate:[NSDate date]];
NSDateComponents *dayComponents = [gregorian components:NSDayCalendarUnit fromDate:fromDate toDate:toDate options:0];
return dayComponents.day;
}

 

人已赞赏
iOS文章

iOS 开发法webview头部加个自定义view,这个view不是固定的而是要跟webview一起滚动

2020-2-26 13:44:21

iOS文章

iOS 自定义UIDatePicker日期选择器视图

2020-2-26 15:21:41

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索