ios - 在日期之间(在实际日期,而不是天数) [英] ios - get in between dates (in actual date, not number of days)

查看:128
本文介绍了ios - 在日期之间(在实际日期,而不是天数)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have two date string and I wanted to get the in between dates. For example,

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

之间的日期将是(26-01-2014,27-01-2014,28-01 -2014 .......)
最好还包括 startDate endDate 。大多数问题,我设法找到要求天数。但我需要它是实际的日期。有没有反正我可以得到之间的日期?

In between dates will be (26-01-2014, 27-01-2014, 28-01-2014.......) preferably include startDate and endDate as well. Most of the question I managed to find asked for number of days. But I needed it to be actual date. Is there anyway that I can get the in between dates?

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);

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

I managed to find this which only returns number of days difference.

推荐答案

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 现在包含 startDate endDate 之间的日期(包括设备时区午夜)。

请注意,在某些时区,这可能会导致麻烦,因为可能会发生从和到夏令时的切换,请参阅 WWDC 2011视频会话117 - 执行日历计算以获取更多信息。一个诀窍是将小时换成保存时间,即中午,进行计算,然后减去12小时。

The array dates now contains the list of dates between startDate and endDate, including those, for midnight in the timezone of the device.
Note, that on some timezones this might cause trouble, as the switch from and to Daylight Saving Time might occur at that moment, see WWDC 2011 Video "Session 117 - Performing Calendar Calculations" for further information. One trick is to shift the hour to a save time, i.e. noon, do the calculation and than subtract 12 hours.

这篇关于ios - 在日期之间(在实际日期,而不是天数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆