【发布时间】:2015-01-30 15:47:34
【问题描述】:
给定一个 NSDate,我想计算 接下来的 n 天。
有没有办法从日历对象获取第二天组件?通过这种方式,我可以编写一个递归函数来获取接下来的 n 个 NSDates,而无需编写我自己的“第二天”函数。
例如:如果今天是 2 月 28 日,我不想编写一个算法来计算今天 + 3 天并根据年份的类型得到 3 月 3 日或 3 月 2 日。相反,我希望有类似的东西:get_next_day from a date。
到目前为止,这是我的代码:
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?
【问题讨论】:
标签: ios nsdate nscalendar