【问题标题】:ios - get in between dates (in actual date, not number of days)ios - 在日期之间进入(实际日期,而不是天数)
【发布时间】:2014-01-08 02:55:27
【问题描述】:

我有两个日期字符串,我想得到两个日期之间的时间。 例如,

NSString *startDate = @"25-01-2014";
NSString *endDate = @"02-02-2014";

在两个日期之间将是 (26-01-2014, 27-01-2014, 28-01-2014.......) 最好也包括startDateendDate。我设法找到的大多数问题都询问了天数。但我需要它是实际日期。无论如何,我可以在两个日期之间获得吗?

NSString *start = @"2010-09-01";
NSString *end = @"2010-12-05";

NSDateFormatter *f = [[NSDateFormatter alloc] init];
[f setDateFormat:@"yyyy-MM-dd"];
NSDate *startDate = [f dateFromString:start];
NSDate *endDate = [f dateFromString:end];

NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [gregorianCalendar components:NSDayCalendarUnit
                                                    fromDate:startDate
                                                      toDate:endDate
                                                     options:0];

NSLog(@"Difference in date components: %d", components.day);

我设法找到这个只返回天数差异。

【问题讨论】:

  • 是否要获取 startDate 和 endDate 之间的日期列表? (开始日期,结束日期)?
  • 是的。我想得到那个日期列表。下面提供的两个答案都是正确的,并返回日期列表。

标签: ios objective-c date calendar nsdatecomponents


【解决方案1】:
NSString *start = @"2010-09-01";
NSString *end = @"2010-12-05";

NSDateFormatter *f = [[NSDateFormatter alloc] init];
[f setDateFormat:@"yyyy-MM-dd"];
NSDate *startDate = [f dateFromString:start];
NSDate *endDate = [f dateFromString:end];

NSMutableArray *dates = [@[startDate] mutableCopy];


NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [gregorianCalendar components:NSDayCalendarUnit
                                                    fromDate:startDate
                                                      toDate:endDate
                                                     options:0];

for (int i = 1; i < components.day; ++i) {
    NSDateComponents *newComponents = [NSDateComponents new];
    newComponents.day = i;

    NSDate *date = [gregorianCalendar dateByAddingComponents:newComponents 
                                                      toDate:startDate 
                                                     options:0];
    [dates addObject:date];
}

[dates addObject:endDate];

数组dates 现在包含startDateendDate 之间的日期列表,包括设备时区午夜的日期。
请注意,在某些时区,这可能会导致麻烦,因为此时可能会发生夏令时的切换,请参阅WWDC 2011 Video "Session 117 - Performing Calendar Calculations" 了解更多信息。一个技巧是将小时转移到节省时间,即中午,进行计算,然后减去 12 小时。

【讨论】:

  • 我对此进行了测试,它确实有效。它返回了日期列表,并感谢您提供有关时区的指针。抱歉,我已经接受了 Duncan C 的回答。但你仍然有我的感激之情。
  • 好吧。实际上你是对的。我向回答这个问题的双方道歉。应该看到时差了……
  • 你救了我的命。完美运行。
【解决方案2】:

好的,您已经完成了大部分工作。您有一个将日期字符串转换为 NSDates 的日期格式化程序。您有日期之间的天数。现在您需要从开始日期循环那多天,将可变天数添加到开始日期。

你需要的方法是dateByAddingComponents:toDate:options:。

类似这样的内容(紧跟在您的代码之后):

  int days = components.day;

  NSDateComponents *daysToAdd = [[NSDateComponents alloc] init];
  for (int count = 0; count <= days; count++)
  {
    daysToAdd.day = count;
    NSDate *loopDate = [gregorianCalendar dateByAddingComponents: daysToAdd
                                                         toDate: startDate
                                                        options: 0 ];
    NSLog(@"Day %d = %@", count+1, [f stringFromDate: loopDate]);
  }

我没有测试过,但这是基本的想法......

上面的代码应该包括开始日期和结束日期。

如果您不想包含开始日期,请让循环从 count = 1 而不是 count = 0 开始。

如果您不想包含结束日期,请将循环检查计数设为

【讨论】:

  • 感谢 Duncan C。它成功了,这返回了我需要的结果。
【解决方案3】:

有点晚了,但找到了 vikingosegundo 在回答中谈到的“TIMEZONE”时间问题的解决方案。我只是通过在 for 循环中的格式化程序 *f 上调用 stringFromDate 方法将 NSDate 转换为 NSString...

for (int i = iStart; i < components.day; ++i) {
    NSDateComponents *newComponents = [NSDateComponents new];
    newComponents.day = i;
    newComponents.hour = 0;
    newComponents.minute = 0;
    newComponents.second = 0;

    NSDate *date = [gregorianCalendar dateByAddingComponents:newComponents
                                                      toDate:startDate
                                                     options:0];
    // THIS IS THE CODE I ADDED TO MINE //
    // convert NSDate to string calling on *f (NSDateformatter)
    NSString *string = [f stringFromDate:date];
    // split the string to separate year, month, etc... 
    NSArray *array = [string componentsSeparatedByString:@"-"];
    // create second NSString withFormat and only add the variable from the date that you find pertinent 
    NSString *sDateNew = [NSString stringWithFormat:@"%@-%@-%@",array[0],array[1],array[2]];
    // then simply add the sDateNew string to dates array
    [dates addObject:sDateNew];
}

我认为这应该放入您的答案 vikingsegundo 的评论部分,但它不会让我这样做。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-08
    • 2015-12-01
    相关资源
    最近更新 更多