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

查看:22
本文介绍了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.......)最好也包括 startDateendDate.我设法找到的大多数问题都询问了天数.但我需要它是实际日期.无论如何,我可以在两个日期之间获得吗?

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 现在包含 startDateendDate 之间的日期列表,包括设备时区午夜的日期列表.
请注意,在某些时区,这可能会导致麻烦,因为此时可能会发生夏令时的切换,请参阅 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天全站免登陆