我想显示当前周,UINavigationBar
然后单击下一个或上一个按钮以显示上一周或下周
例子:
7 月 22 日 - 7 月 28 日 7 月 29 日 - 8 月 4 日
我想显示当前周,UINavigationBar
然后单击下一个或上一个按钮以显示上一周或下周
例子:
7 月 22 日 - 7 月 28 日 7 月 29 日 - 8 月 4 日
要获得一周的第一个日期,您可以执行以下操作:
- (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 上述答案中的建议计算天数:)
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]];
}