如何在ios中获取两个日期之间的天数?
示例:
Input: Start Date: 3-11-2012 End date: 5-11-2012 Output: 3-11-2012 Wednesday 4-11-2012 Thursday 5-11-2012 Friday
谢谢..
答案 0 :(得分:11)
试试这个,
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-MM-dd"];
NSDate *startDate = [df dateFromString:@"2012-07-20"]; // your start date
NSDate *endDate = [NSDate date]; // your end date
NSDateComponents *dayDifference = [[NSDateComponents alloc] init];
NSMutableArray *dates = [[[NSMutableArray alloc] init] autorelease];
NSUInteger dayOffset = 1;
NSDate *nextDate = startDate;
do {
[dates addObject:nextDate];
[dayDifference setDay:dayOffset++];
NSDate *d = [[NSCalendar currentCalendar] dateByAddingComponents:dayDifference toDate:startDate options:0];
nextDate = d;
} while([nextDate compare:endDate] == NSOrderedAscending);
[df setDateStyle:NSDateFormatterFullStyle];
for (NSDate *date in dates) {
NSLog(@"%@", [df stringFromDate:date]);
}
[df release];
输出:
Friday, July 20, 2012
Saturday, July 21, 2012
Sunday, July 22, 2012
Monday, July 23, 2012
Tuesday, July 24, 2012
答案 1 :(得分:1)
检查此解决方案:
NSDateComponents *components;
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"dd-MM-yyyy"];
NSDate *startDate = [dateFormat dateFromString:@"3-11-2012"];
NSDate *endDate = [dateFormat dateFromString:@"5-11-2012"];
[dateFormat setDateFormat:@"dd-MM-yyyy, EEEE"];
components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit fromDate:startDate toDate:endDate options:0];
int days = [components day];
for (int x = 0; x <= days; x++) {
NSLog(@"%@", [dateFormat stringFromDate:startDate]);
startDate = [startDate dateByAddingTimeInterval:(60 * 60 * 24)];
}
输出结果为:
03-11-2012, Saturday
04-11-2012, Sunday
05-11-2012, Monday
答案 2 :(得分:0)
这就是你需要的:
NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
[inputFormatter setDateFormat:@"mm-dd-yyyy"];
NSDate *inputDate = [inputFormatter dateFromString:@"3-11-2012"];
NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setDateFormat:@"mm-dd-yyyy EEEE"];
NSString *output = [outputFormatter stringFromDate:inputDate];
NSLog(@"%@",output);
[inputFormatter release];
[outputFormatter release];
答案 3 :(得分:0)
这是一种方法:
NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
[dateComponents setYear:2012];
[dateComponents setMonth:11];
[dateComponents setDay:03];
NSDate *date1 = [gregorianCalendar dateFromComponents:dateComponents];
[dateComponents setYear:2012];
[dateComponents setMonth:11];
[dateComponents setDay:05];
NSDate *date2 = [gregorianCalendar dateFromComponents:dateComponents];
NSDateFormatter *outputDateFormatter = [[NSDateFormatter alloc] init];
[outputDateFormatter setDateFormat:@"dd-MM-yyyy EEEE"];
NSDate *dayDate;
dayDate = date1;
NSLog(@"%@", [outputDateFormatter stringFromDate:dayDate]);
do {
dayDate = [dayDate dateByAddingTimeInterval:86400];
NSLog(@"%@", [outputDateFormatter stringFromDate:dayDate]);
} while ([dayDate compare:date2]==NSOrderedAscending);
使用NSCalendar为您(和您的用户)提供了更大的显示和操作日期的灵活性。