【问题标题】:How can we get Next Date From Entered Date based on given repeate period ?我们如何根据给定的重复周期从输入的日期获取下一个日期?
【发布时间】:2011-07-13 12:37:33
【问题描述】:

我是 iPhone 新手。

我想根据重复周期从给定日期找出下一个日期。

例如:

我想要的功能如下...

  • 给定日期:2011 年 5 月 31 日重复:每月 作为参数,则应返回下一个日期 2011 年 7 月 31 日 strong>(因为六月没有第 31 天)

  • 如果给定日期:2008 年 2 月 29 日重复:每年 作为参数,函数应该足够聪明,可以计算下一个闰年那么应该返回下一个日期29'Feb 2012(下一个闰年)

  • 等等重复选项可以是以下之一:每日、每周(在选定的星期几)、每月、每年、无(完全不重复)

【问题讨论】:

标签: iphone datetime


【解决方案1】:
// start by retrieving day, weekday, month and year components for yourDate
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *todayComponents = [gregorian components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit) yourDate];
NSInteger theDay = [todayComponents day];
NSInteger theMonth = [todayComponents month];
NSInteger theYear = [todayComponents year];

// now build a NSDate object for yourDate using these components
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setDay:theDay]; 
[components setMonth:theMonth]; 
[components setYear:theYear];
NSDate *thisDate = [gregorian dateFromComponents:components];
[components release];

// now build a NSDate object for the next day
NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
[offsetComponents setDay:1];
NSDate *nextDate = [gregorian dateByAddingComponents:offsetComponents toDate: yourDate options:0];
[offsetComponents release];
[gregorian release];

这是从How can i get next date using NSDate? 复制的,此答案归功于@Massimo Cafaro

【讨论】:

    【解决方案2】:

    要获取明天的日期,请使用dateByAddingTimeInterval 方法。

    // Start with today
    NSDate *today = [NSDate date];
    
    // Add on the number of seconds in a day
    NSTimeInterval oneDay = 60 * 60 * 24;
    NSDate *tomorrow = [today dateByAddingTimeInterval:oneDay];
    

    将其延长至一周等非常简单

    NSTimeInterval oneWeek = oneDay * 7;
    NSDate *nextWeek = [today dateByAddingTimeInterval:oneWeek];
    

    【讨论】:

    • dateByAddingTimeInterval: 并不总是给你你想的结果。转到夏令时(或返回)会将日期移动多于或少于一天。可能还有其他情况,例如各国调整时间以切换时区(这种情况并不经常发生,但今年确实发生了)。你应该使用NSDateComponents 来确保你得到你认为你会得到的日期。
    • 你可以通过在你改变时始终处于一天的中心来解决这个问题,但你是对的,这并不理想:) 而且我的答案处理几个月的移动真的非常糟糕!我很惊讶我什至得到了一票;)@RakeshBhatt 我认为更多的是钱。
    • 对于夏令时,将 24 小时添加到中午可能会为您提供正确的日期,但时间会关闭。至于时区的变化,你甚至不会在正确的日子。国家/地区更改时区的频率不足以使其成为一个常见问题,但考虑到使用 NSDateComponents 时这根本不是问题,我仍然会说使用 dateByAddingTimeInterval: 不是获得第二天的最佳解决方案。 :)
    【解决方案3】:

    试试这个:-

    - (NSDate *)dateFromDaysOffset:(NSInteger)daysOffset
    {
        // start by retrieving day, weekday, month and year components for yourDate
        NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    
        NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
        [offsetComponents setDay:daysOffset];
        NSDate *offsetDate = [gregorian dateByAddingComponents:offsetComponents toDate:self options:0];
        [offsetComponents release];
        [gregorian release];    
    
        return offsetDate;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-13
      • 1970-01-01
      • 1970-01-01
      • 2019-03-31
      • 2023-03-26
      • 1970-01-01
      相关资源
      最近更新 更多