如何实现上个月/下个月的button并显示当前月份的date?

场景:

我有一个跟踪iOS应用程序的费用,我将费用从一个费用明细视图控制器存储到一个表格视图(与提取结果控制器),显示费用列表以及类别和数量和date。 我在我的实体“钱”中有一个date属性,它是一个费用或收入的母公司。

题:

我想要的是基本上每月对我的费用进行分类,并将其显示为节标题,例如:(2012年11月1日 – 2012年11月30日),并根据当月显示费用金额和相关资料。 在这个视图中提供了两个button,如果我按下右边的button,它将使月份增加一个月(2012年12月1日 – 12月31日),同样,左边的button会使月份减less一个月。

我将如何做到这一点? 我正在尝试下面的代码 – 这是有点工作。 假设我有我目前的部分标题(2012年11月1日 – 2012年11月30日),当我按左边的button,它给了我的部分标题(2012年10月1日至10月30日)是错误的,应该是2012年10月31日而不是2012年10月30日。

- (NSDate *)firstDateOfTheMonth { self.startDate = [NSDate date]; NSCalendar* calendar = [NSCalendar currentCalendar]; [calendar setTimeZone:[NSTimeZone localTimeZone]]; NSDateComponents* components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:self.startDate]; [components setDay:1]; firstDateOfTheMonth = [[calendar dateFromComponents:components] retain]; return firstDateOfTheMonth; } - (NSDate *)lastDateOfTheMonth { NSCalendar* calendar = [NSCalendar currentCalendar]; [calendar setTimeZone:[NSTimeZone localTimeZone]]; NSDateComponents* components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:firstDateOfTheMonth]; [components setMonth:[components month]+1]; [components setDay:0]; lastDateOfTheMonth = [[calendar dateFromComponents:components] retain]; return lastDateOfTheMonth; } 

然后我有一个名为“monthCalculation”的方法,我称之为上述两个方法。

 - (void)monthCalculation { [self firstDateOfTheMonth]; [self lastDateOfTheMonth]; } 

现在下面的代码,当我按下左button(减less一个月的月份):

 - (IBAction)showPreviousDates:(id)sender { [self monthCalculation]; NSDateComponents *dateComponents = [[[NSDateComponents alloc] init] autorelease]; [dateComponents setMonth:-1]; NSDate *newDate1 = [[NSCalendar currentCalendar] dateByAddingComponents:dateComponents toDate:firstDateOfTheMonth options:0]; NSDate *newDate2 = [[NSCalendar currentCalendar] dateByAddingComponents:dateComponents toDate:lastDateOfTheMonth options:0]; self.startDate = newDate1; self.endDate = newDate2; NSLog(@" self start date in previous mode =%@", self.startDate); NSLog(@" self end date in previous mode =%@", self.endDate); } 

下面的代码当我按下右键(月份增加一个月):

 - (IBAction)showNextDates:(id)sender { [self monthCalculation]; NSDateComponents *dateComponents = [[[NSDateComponents alloc] init] autorelease]; [dateComponents setMonth:1]; NSDate *newDate1 = [[NSCalendar currentCalendar] dateByAddingComponents:dateComponents toDate:firstDateOfTheMonth options:0]; NSDate *newDate2 = [[NSCalendar currentCalendar] dateByAddingComponents:dateComponents toDate:lastDateOfTheMonth options:0]; self.startDate = newDate1; self.endDate = newDate2; NSLog(@" self start date in previous mode =%@", self.startDate); NSLog(@" self end date in previous mode =%@", self.endDate); } 

我做对了吗? 或者有更好的方法来做到这一点? 任何帮助将不胜感激。

这里有一些方法应该做你想做的事情:

首先,在你的viewDidLoad方法中添加:

 self.startDate = [NSDate date]; [self updateDateRange]; 

这给你一个起点。 然后,我添加了以下五个方法(注意: previousMonthButtonPressed / nextMonthButtonPressed应该连线到您的button):

 // Return the first day of the month for the month that 'date' falls in: - (NSDate *)firstDayOfMonthForDate:(NSDate *)date { NSCalendar *cal = [NSCalendar currentCalendar]; NSDateComponents *comps = [cal components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:date]; comps.day = 1; return [cal dateFromComponents:comps]; } // Return the last day of the month for the month that 'date' falls in: - (NSDate *)lastDayOfMonthForDate:(NSDate *)date { NSCalendar *cal = [NSCalendar currentCalendar]; NSDateComponents *comps = [cal components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:date]; comps.month += 1; comps.day = 0; return [cal dateFromComponents:comps]; } // Move the start date back one month - (IBAction)previousMonthButtonPressed:(id)sender { NSCalendar *cal = [NSCalendar currentCalendar]; NSDateComponents *comps = [cal components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:self.startDate]; comps.month -= 1; self.startDate = [cal dateFromComponents:comps]; [self updateDateRange]; } // Move the start date forward one month - (IBAction)nextMonthButtonPressed:(id)sender { NSCalendar *cal = [NSCalendar currentCalendar]; NSDateComponents *comps = [cal components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:self.startDate]; comps.month += 1; self.startDate = [cal dateFromComponents:comps]; [self updateDateRange]; } // Print the new range of dates. - (void)updateDateRange { NSLog(@"First day of month: %@", [self firstDayOfMonthForDate:self.startDate]); NSLog(@"Last day of month: %@", [self lastDayOfMonthForDate:self.startDate]); } 

尝试这个:

 - (NSDate *)lastDateOfTheMonth { NSCalendar* calendar = [NSCalendar currentCalendar]; [calendar setTimeZone:[NSTimeZone localTimeZone]]; NSDateComponents* components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:firstDateOfTheMonth]; [components setMonth:[components month]+1]; NSDateComponents* offset = [[[NSDateComponents alloc] init] retain]; [offset setDay:-1]; lastDateOfTheMonth = [[calendar dateByAddingComponents:offset toDate:[components date] options:0] retain]; return lastDateOfTheMonth; }