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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:42:50+00:00 2026-06-09T03:42:50+00:00

I am making an iPhone count down app and need to know how many

  • 0

I am making an iPhone count down app and need to know how many times a weekend (like Saturday and Sundays ) will happen until the date the user sets.

An example is how could I find how many weekends will occur from now (Monday, August 6th) until next week? I know the answer is 1 weekend, but I need to be able to figure this out using code.

The closest I have gotten is using the NSDateComponents and NSCalender and doing something close to the following.

NSDateComponents *weekendsLeftComponents = [calendar components:NSWeekCalendarUnit
                                                    fromDate:targetDate
                                                      toDate:[NSDate date]
                                                     options:0];

But from there I have hit the dreaded coders wall of no passing.

My problem is not exactly knowing how to go about doing this. It sounds like an Array could work, but I am afraid the Array could get rather large as some of these “things” could go as long as a couple years. (I am making an app that finds out how many days, weekends, etc until a person graduates.) Also my audience is mainly kids to teens with iPods, older ones too. I don’t know how full their memory (ram) can get before running out.

Thank you so much in advance,

Alex Kafer

  • 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-09T03:42:51+00:00Added an answer on June 9, 2026 at 3:42 am

    It looks like your fromDate and toDate are backwards, assuming targetDate is in the future.

    Here’s how I’d solve this problem.

    Create a category on NSCalendar to count the number of times a specific weekday occurs between two dates:

    @interface NSCalendar (AlexCategory)
    
    // The number of times weekday number `weekday` (1 = Sunday, 7 = Saturday)
    // occurs between `fromDate` and `toDate`.  If `fromDate` falls on the desired
    // weekday, it is counted.  If `toDate` falls on the desired weekday, it is NOT counted.
    - (NSInteger)countOfWeekday:(NSInteger)weekday fromDate:(NSDate *)fromDate toDate:(NSDate *)toDate;
    
    @end
    

    To implement this new method, we’ll start by getting the year, month, day, and weekday of the fromDate:

    @implementation NSCalendar (AlexCategory)
    
    - (NSInteger)countOfWeekday:(NSInteger)weekday fromDate:(NSDate *)fromDate toDate:(NSDate *)toDate {
        NSDateComponents *components = [self components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit | NSWeekdayCalendarUnit fromDate:fromDate];
    

    Next, we count how many days from fromDate to the next desired weekday:

        NSInteger daysUntilDesiredWeekday = weekday - components.weekday;
        // If fromDate is a Wednesday and weekday is Monday (for example),
        // daysUntilDesiredWeekday is negative.  Fix that.
        NSRange weekdayRange = [self minimumRangeOfUnit:NSWeekdayCalendarUnit];
        if (daysUntilDesiredWeekday < weekdayRange.location) {
            daysUntilDesiredWeekday += weekdayRange.length;
        }
    

    Note that daysUntilDesiredWeekday will be zero if fromDate falls on the desired weekday.

    Now we can create an NSDateComponents representing the first desired weekday on or after fromDate:

        NSDateComponents *firstDesiredWeekday = [[NSDateComponents alloc] init];
        firstDesiredWeekday.year = components.year;
        firstDesiredWeekday.month = components.month;
        firstDesiredWeekday.day = components.day + daysUntilDesiredWeekday;
    

    We update fromDate to be that first desired weekday, and return 0 if it’s on or after toDate:

        fromDate = [self dateFromComponents:firstDesiredWeekday];
        if ([fromDate compare:toDate] != NSOrderedAscending) {
            return 0;
        }
    

    Next we count all the days (not just desired weekdays) from the updated fromDate to toDate:

        NSInteger allDaysCount = [self components:NSDayCalendarUnit
            fromDate:fromDate toDate:toDate options:0].day;
    

    We can divide that by the number of days in a week to count just the number of desired weekdays. Since we started counting from a desired weekday, any partial week remainder would also contain the desired weekday, so we need to round up:

        // Adding weekdayRange.length - 1 makes the integer division round up.
        return (allDaysCount + weekdayRange.length - 1) / weekdayRange.length;
    }
    
    @end
    

    We can test the method like this:

    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    
    NSDateComponents *components = [[NSDateComponents alloc] init];
    components.year = 2012;
    components.month = 1;
    components.day = 1;
    NSDate *fromDate = [calendar dateFromComponents:components];
    
    for (NSUInteger i = 1; i <= 365; ++i) {
        components.day = i;
        NSDate *toDate = [calendar dateFromComponents:components];
        NSLog(@"%@ to %@: %ld Mondays", fromDate, toDate, [calendar countOfWeekday:2 fromDate:fromDate toDate:toDate]);
    }
    

    The output starts like this:

    2012-08-06 16:27:05.751 tuesdays[81152:403] 0 Mondays from 2012-01-01 06:00:00 +0000 to 2012-01-01 06:00:00 +0000
    2012-08-06 16:27:05.754 tuesdays[81152:403] 0 Mondays from 2012-01-01 06:00:00 +0000 to 2012-01-02 06:00:00 +0000
    2012-08-06 16:27:05.756 tuesdays[81152:403] 1 Mondays from 2012-01-01 06:00:00 +0000 to 2012-01-03 06:00:00 +0000
    2012-08-06 16:27:05.758 tuesdays[81152:403] 1 Mondays from 2012-01-01 06:00:00 +0000 to 2012-01-04 06:00:00 +0000
    2012-08-06 16:27:05.759 tuesdays[81152:403] 1 Mondays from 2012-01-01 06:00:00 +0000 to 2012-01-05 06:00:00 +0000
    2012-08-06 16:27:05.760 tuesdays[81152:403] 1 Mondays from 2012-01-01 06:00:00 +0000 to 2012-01-06 06:00:00 +0000
    2012-08-06 16:27:05.762 tuesdays[81152:403] 1 Mondays from 2012-01-01 06:00:00 +0000 to 2012-01-07 06:00:00 +0000
    2012-08-06 16:27:05.763 tuesdays[81152:403] 1 Mondays from 2012-01-01 06:00:00 +0000 to 2012-01-08 06:00:00 +0000
    2012-08-06 16:27:05.763 tuesdays[81152:403] 1 Mondays from 2012-01-01 06:00:00 +0000 to 2012-01-09 06:00:00 +0000
    2012-08-06 16:27:05.764 tuesdays[81152:403] 2 Mondays from 2012-01-01 06:00:00 +0000 to 2012-01-10 06:00:00 +0000
    2012-08-06 16:27:05.765 tuesdays[81152:403] 2 Mondays from 2012-01-01 06:00:00 +0000 to 2012-01-11 06:00:00 +0000
    

    This looks correct according to the output of cal 1 2012:

        January 2012
    Su Mo Tu We Th Fr Sa
     1  2  3  4  5  6  7
     8  9 10 11 12 13 14
    15 16 17 18 19 20 21
    22 23 24 25 26 27 28
    29 30 31
    

    (Remember, the toDate is not counted, so there are 0 Mondays from 2012-1-1 to 2012-1-2, even though 2012-1-2 is a Monday.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am making an iPhone app wherein I need users to enter their email
I'm making count down timer for iPhone and Android. The problem is alert message.
im making a iphone app so that any text in the detailTextlabel will fit
I am making an iPhone app and I need it to be in portrait
I am making an iPhone game. I have all the required app sizes except
I'm making an iPhone app that has information about different video games. How can
I am making a iPhone app that has two different targets. They use the
I'm currently making an iphone web app based on Google App Engine (python). I
I'm currently making an iPhone app that has PDF viewing a crucial part of
I'm making an iphone app that uses NSURLConnection to download some data from the

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.