gpt4 book ai didi

objective-c - 获取iOS中两个日期之间的日期名称?

转载 作者:行者123 更新时间:2023-12-01 17:18:37 26 4
gpt4 key购买 nike

如何获取iOS中两个日期之间的日期名称?

例:

输入:
开始日期:3-11-2012
结束日期:2012年5月11日

输出:
2012/3/11星期三
2012年4月11日星期四
5-11-2012星期五

谢谢..

最佳答案

尝试这个,

    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

关于objective-c - 获取iOS中两个日期之间的日期名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11624508/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com