How do I implement previous/next month buttons and show dates for current month?

前端 未结 2 1598
囚心锁ツ
囚心锁ツ 2021-01-16 20:24

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 controll

2条回答
  •  滥情空心
    2021-01-16 20:54

    Try this:

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

提交回复
热议问题