Display NSDates in terms of week

风格不统一 提交于 2019-12-10 11:48:08

问题


Scenario:

I have an expense tracking iOS Application and I am storing expenses from a expense detail view controller into a table view (with fetched results controller) that shows the list of expenses along with the category and amount and date. I do have a date attribute in my entity "Money" which is a parent entity for either an expense or an income.

Question:

What I want is to basically categorize my expenses for a given week, a month, or year and display it as the section header title for example : (Oct 1- Oct 7, 2012) and it shows expenses amount and related stuff according to that particular week. Two buttons are provided in that view, if I would press the right button, it will increment the week by a week (Oct 1- Oct 7, 2012 now shows Oct8 - Oct 15, 2012) and similarly the left button would decrement the week by a week.

How would I accomplish that? I am trying the following code - doesn't work.

- (void)weekCalculation
{
   NSDate *today = [NSDate date];  // present date (current date)

   NSCalendar* calendar = [NSCalendar currentCalendar];
   NSDateComponents* comps = [calendar components:NSYearForWeekOfYearCalendarUnit |NSYearCalendarUnit|NSMonthCalendarUnit|NSWeekCalendarUnit|NSWeekdayCalendarUnit fromDate:today];

   [comps setWeekday:1]; // 1: Sunday
   firstDateOfTheWeek = [[calendar dateFromComponents:comps] retain];
   [comps setWeekday:7]; // 7: Saturday
   lastDateOfTheWeek = [[calendar dateFromComponents:comps] retain];

   NSLog(@" first date of week =%@", firstDateOfTheWeek);
   NSLog(@" last date of week =%@", lastDateOfTheWeek);

   firstDateOfWeek = [dateFormatter stringFromDate:firstDateOfTheWeek];
   lastDateOfWeek = [dateFormatter stringFromDate:lastDateOfTheWeek];

}

Code for incrementing date -

- (IBAction)showNextDates:(id)sender
{
   int addDaysCount = 7;

   NSDateComponents *dateComponents = [[[NSDateComponents alloc] init] autorelease];
   [dateComponents setDay:addDaysCount];

   NSDate *newDate1 = [[NSCalendar currentCalendar]
                    dateByAddingComponents:dateComponents
                    toDate:firstDateOfTheWeek options:0];

   NSDate *newDate2 = [[NSCalendar currentCalendar]
                    dateByAddingComponents:dateComponents
                    toDate:lastDateOfTheWeek options:0];

   NSLog(@" new dates =%@ %@", newDate1, newDate2);

}

Suppose the week shows like this (Nov4, 2012 - Nov10, 2012) and I press the increment button, I see in the console, date changes to Nov11,2012 and Nov.17, 2012 which is right but if I press the increment button again, it shows the same date again (Nov 11, 2012 and Nov.17, 2012).

Please help me out here.


回答1:


Declare currentDate as an @property in your class. And try this.

@property(nonatomic, retain) NSDate *currentDate;

Initially set

self.currentDate = [NSDate date];

before calling this method.

- (void)weekCalculation
{
   NSCalendar* calendar = [NSCalendar currentCalendar];
   NSDateComponents* comps = [calendar components:NSYearForWeekOfYearCalendarUnit |NSYearCalendarUnit|NSMonthCalendarUnit|NSWeekCalendarUnit|NSWeekdayCalendarUnit fromDate:self.currentDate];

   [comps setWeekday:1]; // 1: Sunday
   firstDateOfTheWeek = [[calendar dateFromComponents:comps] retain];
   [comps setWeekday:7]; // 7: Saturday
   lastDateOfTheWeek = [[calendar dateFromComponents:comps] retain];

   NSLog(@" first date of week =%@", firstDateOfTheWeek);
   NSLog(@" last date of week =%@", lastDateOfTheWeek);

   firstDateOfWeek = [dateFormatter stringFromDate:firstDateOfTheWeek];
   lastDateOfWeek = [dateFormatter stringFromDate:lastDateOfTheWeek];

}

Once the view is loaded self.currentDate value should be updated from showNextDates. Make sure it is not getting reset anywhere else.

- (IBAction)showNextDates:(id)sender
{
   int addDaysCount = 7;

   NSDateComponents *dateComponents = [[[NSDateComponents alloc] init] autorelease];
   [dateComponents setDay:addDaysCount];

   NSDate *newDate1 = [[NSCalendar currentCalendar]
                    dateByAddingComponents:dateComponents
                    toDate:firstDateOfTheWeek options:0];

   NSDate *newDate2 = [[NSCalendar currentCalendar]
                    dateByAddingComponents:dateComponents
                    toDate:lastDateOfTheWeek options:0];

   NSLog(@" new dates =%@ %@", newDate1, newDate2);

   self.currentDate = newDate1;

}



回答2:


I had needed similar thing in one of my old projects and achieved it via the code below. It sets this weeks date to an NSDate variable and adds/removes 7 days from day component in each button click. Here is the code:

NSCalendar *calendar = [NSCalendar currentCalendar];
[calendar setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
NSDate *date = [NSDate date];
NSDateComponents *components = [calendar components:(NSYearCalendarUnit|NSWeekdayCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit)  fromDate:date ];
//
[components setDay:([components day] - ([components weekday] )+2)];
self.currentDate = [calendar dateFromComponents:components];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"dd.MM.yyyy HH:mm:ss";
self.datelabel.text = [formatter stringFromDate:self.currentDate];

The code above calculates this week start and sets it to currentDate variable. I Have two UIButtons with UIActions named prevClick and nextClick which calls the method that sets the next or previous weekstart:

- (IBAction)prevClick:(id)sender {
[self addRemoveWeek:NO];

}

-(void)addRemoveWeek:(BOOL)add{
NSCalendar *calendar = [NSCalendar currentCalendar];
[calendar setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
NSDateComponents *components = [calendar components:(NSYearCalendarUnit|NSWeekdayCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit)  fromDate:self.currentDate ];
components.day = add?components.day+7:components.day-7;
self.currentDate = [calendar dateFromComponents:components];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"dd.MM.yyyy HH:mm:ss";
self.datelabel.text = [formatter stringFromDate:self.currentDate];

}

- (IBAction)nextClk:(id)sender {
[self addRemoveWeek: YES];

}



来源:https://stackoverflow.com/questions/13303909/display-nsdates-in-terms-of-week

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!