1

iOSで2つの日付の間の日の名前を取得するには?

例:

入力:
 開始日: 2012 年 3 月 11 日
 終了日: 2012 年 5 月 11 日

出力:
 2012 年 3 月 11 日水曜日
 2012 年 4 月 11 日木曜日
 2012 年 5 月 11 日金曜日

ありがとう..

4

4 に答える 4

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
于 2012-07-24T05:45:11.880 に答える
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
于 2012-07-24T05:45:47.013 に答える
1

これを行う1つの方法は次のとおりです。

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 を使用すると、日付の表示と操作の柔軟性が高まります。

于 2012-07-24T05:58:40.180 に答える
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];
于 2012-07-24T05:42:59.327 に答える