【问题标题】:NSDate and NSCalendar: is there a get next day function? [duplicate]NSDate 和 NSCalendar:是否有获取次日功能? [复制]
【发布时间】: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


【解决方案1】:

您需要通过日期组件来完成。所以,

components.day = components.day + 1;
newDate = [[NSCalendar currentCalendar] dateFromComponents:components]; 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-23
    • 1970-01-01
    相关资源
    最近更新 更多