Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8848201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:22:16+00:00 2026-06-14T12:22:16+00:00

Scenario: I have an expense tracking iOS Application and I am storing expenses from

  • 0

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 on a monthly basis and display it as the section header title for example : (Nov 1 – Nov 30,2012) and it shows expenses amount and related stuff according to that particular month. Two buttons are provided in that view, if I would press the right button, it will increment the month by a month (Dec 1 – Dec 31, 2012) and similarly the left button would decrement the month by a month.

How would I accomplish that? I am trying the following code – which is kinda working. Suppose I have my current section header as (Nov1 – Nov 30, 2012) and when I press the left button, it gives me the section header (Oct 1 – Oct 30, 2012) which is wrong, it should be Oct 31, 2012 and not Oct 30, 2012.

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

Then I have a method called “monthCalculation” in which I call the above two methods.

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

Now the following code when I press the left button (to decrement the month by a month):

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

The following code when I press the right button (to increment the month by a month):

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

Am I doing it right? or there is a better way to do achieve this? Any help will be appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T12:22:17+00:00Added an answer on June 14, 2026 at 12:22 pm

    Here are some methods that should do what you want:

    First, in your viewDidLoad method add:

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

    This gives you a starting point. Then, I added the following five methods (Note: previousMonthButtonPressed/nextMonthButtonPressed should be wired up to your buttons):

    // 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]);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Scenario: I have an expense tracking iOS Application and I am storing expenses from
Scenario: I have an expense tracking iOS Application and I am storing expenses from
Scenario: I have an expense tracking iOS Application and I am storing expenses from
Scenario : I have an expense tracking iOS Application and I am storing expenses
I have an expense tracking iOS Application using Core Data Model: Scenario: -> An
Scenario I have a 10 million row table. I partition it into 10 partitions,
Scenario: I have an application that pulls data from a SQL database as well
Scenario: I have an application that has a config table which stores the config
Scenario : I have a users table in my application. I also have two
Scenario: I have Window Application to be installed/updated from website. User needs to enter

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.