给定一个NSDate,我想计算接下来的n天。
有没有办法从日历对象获取第二天的组件?通过这种方式,我可以编写一个递归函数来获取接下来的 n 个 NSDate,而无需编写我自己的"第二天"函数。
例如:如果今天是 2 月 28 日,我不想编写一个算法来计算今天 + 3 天并根据年份类型获得 3 月 3 日或 3 月 2 日。相反,我想要这样的东西:约会get_next_day。
这是我到目前为止的代码:
NSDate * now = [NSDate date];
NSCalendar * calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:now];
NSInteger day = [components day];
NSInteger month = [components month];
NSInteger year = [components year];
// Is there a way to get the next day without having to breakdown the components and compute it myself?
您需要通过日期组件来完成。所以
components.day = components.day + 1;
newDate = [[NSCalendar currentCalendar] dateFromComponents:components];