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 8820499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:37:23+00:00 2026-06-14T05:37:23+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 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 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-14T05:37:24+00:00Added an answer on June 14, 2026 at 5:37 am

    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;
    
    }
    
    • 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
I have an expense tracking iOS Application using Core Data Model: Scenario: -> An
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
Scenario: I have a console application that needs to access a network share with
Scenario I have recently graduated from university with a degree in Computer Science. My

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.