0

来週の最終日を取得したい。ここにmが使用しているコードがあります..

NSCalendar *gregorian11 = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];  
NSDateComponents *components1 = [gregorian11 components:NSWeekdayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:today1];
[components1 setDay:([components1 day]-([components1 weekday]-1)+14)];

NSDate *lastdayOfNextWeek = [gregorian11 dateFromComponents:components1];
NSDateFormatter *dateFormat_first = [[NSDateFormatter alloc] init];
[dateFormat_first setDateFormat:@"dd/MM/yyyy :EEEE"];
NSString *dateString_first = [dateFormat_first stringFromDate:beginningOfWeek1];

NSLog(@"last_date: %@", dateString_first);

出力:last_date: 30/09/2012 :Sunday

今、私はこれが正しい方法なのか、それとももっと良い解決策があるのか​​ を知りたいですか??

4

1 に答える 1

0
Hey Check out this Code.....:)


 //Week End Date

    NSCalendar *gregorianEnd = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

    NSDateComponents *componentsEnd = [gregorianEnd components:NSWeekdayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:today];

    int Enddayofweek = [[[NSCalendar currentCalendar] components:NSWeekdayCalendarUnit fromDate:today] weekday];// this will give you current day of week

    [componentsEnd setDay:([componentsEnd day]+(7-Enddayofweek)+1)];// for end day of the week

    NSDate *EndOfWeek = [gregorianEnd dateFromComponents:componentsEnd];
    NSDateFormatter *dateFormat_End = [[NSDateFormatter alloc] init];
    [dateFormat_End setDateFormat:@"yyyy-MM-dd"];
    dateEndPrev = [dateFormat stringFromDate:EndOfWeek];

    weekEndPrev = [[dateFormat_End dateFromString:dateEndPrev] retain];

    //Convert the nsdate to nsstirng for Query

    strWeekEndQuery =[dateFormat stringFromDate:weekEndPrev];
    NSLog(@"%@",strWeekEndQuery);
于 2012-09-17T12:45:35.447 に答える