显示当前的开始和结束日期

时间:2022-09-26 11:14:07

I want to display the Current week in UINavigationBar and then click on Next or Previous button to display the Previous week or Next week

我想在UINavigationBar中显示当前周,然后单击Next或Previous按钮以显示上周或下周

Example:

July 22 - July 28
July 29 - Auguest 4

2 个解决方案

#1


1  

To get first date of the week you can do something like this :

要获得本周的第一个日期,您可以执行以下操作:

- (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];
}

After getting first week of the day you can calculate days as suggested in above answer by @vikas :)

在获得当天的第一周后,您可以按照以上答案中的建议计算天数@vikas :)

#2


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

}

#1


1  

To get first date of the week you can do something like this :

要获得本周的第一个日期,您可以执行以下操作:

- (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];
}

After getting first week of the day you can calculate days as suggested in above answer by @vikas :)

在获得当天的第一周后,您可以按照以上答案中的建议计算天数@vikas :)

#2


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

}