2

の現在の週を表示してUINavigationBarから、[次へ]または[前の週]ボタンをクリックして、前の週または次の週を表示したい

例:

7月22日〜7月28日
7月29日-8月4日
4

2 に答える 2

1

週の最初の日付を取得するには、次のようにします。

- (NSDate *)firstDayOfWeekFromDate:(NSDate *)date {
    CFCalendarRef currentCalendar = CFCalendarCopyCurrent();
    NSDateComponents *components = [CURRENT_CALENDAR components:DATE_COMPONENTS  fromDate:date];
    [components setDay:([components day] - ([components weekday] - CFCalendarGetFirstWeekday(currentCalendar)))];
    //[components setDay:(CFCalendarGetFirstWeekday(currentCalendar))];
    [components setHour:0];
    [components setMinute:0];
    [components setSecond:0];
    CFRelease(currentCalendar);
    return [CURRENT_CALENDAR dateFromComponents:components];
}

その日の最初の週を取得した後、@vikasによる上記の回答で提案されているように日数を計算できます:)

于 2012-07-25T12:28:50.840 に答える
0
 NSDate * selectedDate = [NSDate date];


-(IBAction)Week_CalendarActionEvents:(id)sender{

NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDateComponents *offsetComponents = [[[NSDateComponents alloc] init] autorelease];
NSDate *nextDate;

if(sender==Week_prevBarBtn)  // Previous button events 
    [offsetComponents setDay:-7];
else if(sender==Week_nextBarBtn) // next button events 
    [offsetComponents setDay:7];

nextDate = [gregorian dateByAddingComponents:offsetComponents toDate:selectedDate options:0];

selectedDate = nextDate;
[selectedDate retain];

NSDateComponents *components = [gregorian components:NSWeekCalendarUnit fromDate:selectedDate];
NSInteger week = [components week];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"MMMM YYYY"];
NSString *stringFromDate = [formatter stringFromDate:selectedDate];
[formatter release];
[Week_weekBarBtn setTitle:[NSString stringWithFormat:@"%@,Week %d",stringFromDate,week]];

}

于 2012-07-25T11:42:55.793 に答える