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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:37:11+00:00 2026-06-05T05:37:11+00:00

I would like to have a function that takes a week number as input

  • 0

I would like to have a function that takes a week number as input and returns an array of all the NSDates that this specific week is made by.

Something like this:

-(NSArray*)allDatesInWeek:(int)weekNumber {
    NSDate *today = [NSDate date];
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    [calendar setFirstWeekday:2];
    NSDateComponents *todayComp = [calendar components:NSYearCalendarUnit fromDate:today];
    int currentyear = todayComp.year;
    /* Calculate and return the date of weekNumber for current year */

}

How can this be done in a simple way?

  • 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-05T05:37:12+00:00Added an answer on June 5, 2026 at 5:37 am

    Tested.

    -(NSArray*)allDatesInWeek:(int)weekNumber {
       // determine weekday of first day of year: 
       NSCalendar *greg = [[NSCalendar alloc] 
          initWithCalendarIdentifier:NSGregorianCalendar];
       NSDateComponents *coms = [[NSDateComponents alloc] init];
       comps.day = 1;
       NSDate *today = [NSDate date];
       NSDate *tomorrow = [greg dateByAddingComponents:comps toDate:today];
       const NSTimeInterval kDay = [tomorrow timeIntervalSinceDate:today];
       comps = [greg components:NSYearCalendarUnit fromDate:[NSDate date]];
       comps.day = 1;
       comps.month = 1; 
       comps.hour = 12;
       NSDate *start = [greg dateFromComponents:comps];
       comps = [greg components:NSWeekdayCalendarUnit fromDate:start]; 
       if (weekNumber==1) {
           start = [start dateByAddingTimeInterval:-kDay*(comps.weekday-1)];
       } else {
           start = [start dateByAddingTimeInterval:
              kDay*(8-comps.weekday+7*(weekNumber-2))];
       }
       NSMutableArray *result = [NSMutableArray array];
       for (int i = 0; i<7; i++) {
           [result addObject:[start dateByAddingTimeInterval:kDay*i]];
       }
       return [NSArray arrayWithArray:result];
    }
    

    This assumes the first day of the week is Sunday, as stated in the NSDate API. Tweak if desired.

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

Sidebar

Related Questions

I would like to have a function that takes an array as input and
I have a dataframe and I would like to apply a function that takes
I would like to implement a function that takes as input a size n
I would like an algorithm for a function that takes n integers and returns
I have C function that takes string pointer as a parameter. This functions returns
I currently have a function that takes a csv file, and returns an array
I would like to build a function that takes a multidimensional array and prints
I have a function that takes a ref parameter and would like to use
I would like to have a function that can wrap any other function call.
I have a function that I would like to return 0 if a certain

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.