2012-09-17 92 views
0

我想獲得下週的最後一天。這裏使用的M代碼..下週在iPhone中的最後日期

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); 

OUTPUT:last_date: 30/09/2012 :Sunday

現在,我想知道這是一個正確的方式還是你們有更好的解決這個?

+0

參考http://stackoverflow.com/questions/2719235/how-to-find-last-day-of-current-week-in-iphone – Maulik

回答

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); 
相關問題