【问题标题】:Adding a day to current date in iOS在 iOS 中为当前日期添加一天
【发布时间】:2013-06-21 08:29:35
【问题描述】:

我看到了这个帖子:iOS and finding Tomorrow

提供的代码是:

units = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
NSDateComponents* comps = [[NSCalendar currentCalendar] components:units fromDate:[NSDate date]]; 
// Add one day
comps.day = comps.day+1; // no worries: even if it is the end of the month it will wrap to the next month, see doc
// Recompose a new date, without any time information (so this will be at midnight)
NSDate* tomorrowMidnight = [[NSCalendar currentCalendar] dateFromComponents:comps];

问题:我需要在当前日期上添加一天,例如,2013 年 6 月 21 日和 2013 年 5 月 21 日之间的差异是 1 个月而不是 0 个月。

我正在使用的代码:

    NSDate *selectedDate = [picker2 date];
    unsigned int unitFlags = NSHourCalendarUnit | NSMinuteCalendarUnit | NSDayCalendarUnit | NSMonthCalendarUnit;
    NSDateComponents *conversionInfo = [currCalendar components:unitFlags fromDate:selectedDate toDate:[NSDate date]  options:0];
    DLog(@"%i",conversionInfo.month);
    DLog(@"%i",conversionInfo.day);
    conversionInfo.day +=1;
    DLog(@"%i",conversionInfo.day);
    DLog(@"%i",conversionInfo.month);
    int months = [conversionInfo month];

但是当我试图获得 2013 年 6 月 21 日和 2013 年 5 月 21 日之间的差异时 -> 仍然返回 0 个月而不是 1 个月。

需要一些帮助。

【问题讨论】:

  • 如果您只想在今天添加一天,请执行以下操作:NSDate *tomorrow = [NSDate dateWithTimeInterval:(24*60*60) sinceDate:[NSDate date]]; 这会将给定日期增加 24*60*60 秒并返回一个新日期。
  • 感谢您的快速回复。工作...添加为答案..将其标记为正确..
  • 我不明白你问题的最后一句话。为什么 2013 年 6 月 21 日和 2013 年 6 月 20 日的差是 1 个月?
  • 对最后一部分道歉.. 输入错误...
  • @lakesh:好的。但是 2013 年 6 月 21 日和 2013 年 5 月 21 日之间的差异 一个月(我测试过)。问题可能是两个日期的时间不同。例如,“2013 年 6 月 21 日 00:00”和“2013 年 5 月 21 日 00:30”之间的差异给出了 30 天、23 小时、30 分钟和 0 个月。因此,也许您必须先将日期标准化为午夜。

标签: ios objective-c date


【解决方案1】:

形成一个日期组件,其中包含要添加到原始日期的天数。通过将此组件添加到您的原始日期,从当前日历形成日期。

NSDateComponents *dateComponents = [NSDateComponents new];
dateComponents.day = 1;
NSDate *newDate = [[NSCalendar currentCalendar]dateByAddingComponents:dateComponents 
                                                               toDate: selectedDate 
                                                              options:0];

【讨论】:

    【解决方案2】:

    这是我使用的一种方法:

    + (NSDate *)addDays:(NSInteger)days toDate:(NSDate *)originalDate {
        NSDateComponents *components= [[NSDateComponents alloc] init];
        [components setDay:days];
        NSCalendar *calendar = [NSCalendar currentCalendar];
        return [calendar dateByAddingComponents:components toDate:originalDate options:0];
    }
    

    有了它,您可以添加任意天数。它也适用于负数,因此您可以减去天数。

    【讨论】:

      【解决方案3】:

      简单直接的解决方案:

      NSDate *currentDate = [NSDate date];
      
      NSDate *nextDate = [currentDate dateByAddingTimeInterval:(24*3600)]; 
      

      这对我来说很好。

      【讨论】:

      • 一天并不总是有 86400 秒。在转换为夏令时或从夏令时转换时,此计算可能会给出错误的结果。 - (相同的答案已经给出,稍后删除。)
      【解决方案4】:

      对于快速的谷歌用户:

      NSCalendar.currentCalendar().dateByAddingUnit(NSCalendarUnit.CalendarUnitDay, value: 1, toDate: date, options: nil)!
      

      【讨论】:

        【解决方案5】:

        第一件事,您将日期而不是月份分配给 NSDateComponent。 执行以下代码,找出 inDate 和 newDate 的区别

        NSCalendar *calendar = [NSCalendar currentCalendar];
        NSDateComponents *components = [calendar components:NSCalendarUnitMonth fromDate:inDate];
        

        NSInteger monthFlag = 1; components.month = minuteFlag;

        // Retrieve date with increased days count
        NSDate *newDate = [[NSCalendar currentCalendar]
                           dateByAddingComponents:components
                                           toDate:monthFlag
                                          options:0];
        

        【讨论】:

          【解决方案6】:

          要向日期添加任意数量的任意组成部分,例如:2 天或 2 个月、1 年,请执行以下操作:

          Calendar.current.date(byAdding: DateComponents(month: -1), to: date)! //this create date with previous month for date.
          

          你可以用任何东西初始化DateComponents

          public init(calendar: Calendar? = default, timeZone: TimeZone? = default, era: Int? = default, year: Int? = default, month: Int? = default, day: Int? = default, hour: Int? = default, minute: Int? = default, second: Int? = default, nanosecond: Int? = default, weekday: Int? = default, weekdayOrdinal: Int? = default, quarter: Int? = default, weekOfMonth: Int? = default, weekOfYear: Int? = default, yearForWeekOfYear: Int? = default)
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2022-08-18
            • 1970-01-01
            • 2019-10-19
            • 2012-04-16
            • 2014-05-25
            相关资源
            最近更新 更多