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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:49:47+00:00 2026-06-10T17:49:47+00:00

Unsure how to best achieve this. NSDate *date = [NSDate date]; I need to

  • 0

Unsure how to best achieve this.

 NSDate *date = [NSDate date]; 

I need to do a lookup on the date and return a string value.

12/17/2011 < date < 12/23/2011  return "20120101"

12/24/2011 < date < 12/30/2012   return "20120102"

12/31/2011 < date < 01/06/2012   return "20120201"

…

10/20/2012 < date < 10/26/2012  return "20122301"

…

11/02/2013 < date < 11/08/2013   return "20132301"

..

for 5 years… for each week

date can be any date until Dec. 2017.

I do not know the logic behind the return strings so I can’t simply calculate the string based on the date. The return string (converted to NSDate in the model) is used successfully as my section for my fetchedresultscontroller.

I am not sure how to create a lookup table based on an NSDate or if I need some monster if/case statement.

  • 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-10T17:49:49+00:00Added an answer on June 10, 2026 at 5:49 pm

    I would calculate the “week number” of the date in question, and then get the value from an array of strings. This should work for you:

    // Create an array of your strings.
    // This would probably be best to read from a file since you have so many
    NSArray *strings                = [NSArray arrayWithObjects:
                                       @"20120101",
                                       @"20120102",
                                       @"20120201",
                                       @"20122301",
                                       @"20132301", nil];
    
    // Create a new date formatter so that we can create our dates.
    NSDateFormatter *formatter      = [NSDateFormatter new];
    formatter.dateFormat            = @"MM/dd/yyyy";
    
    // Create the date of the first entry in strings.
    // We will be using this as our starting date and will calculate the
    // number of weeks that has elapsed since then.
    NSDate *earliestDate            = [formatter dateFromString:@"12/17/2011"];
    
    // The date to check
    NSDate *dateToCheck             = [formatter dateFromString:@"01/12/2012"];
    
    // Create a calendar to do our calculations for us.
    NSCalendar *cal                 = [NSCalendar currentCalendar];
    
    // Calculate the number of weeks between the earliestDate and dateToCheck
    NSDateComponents *components    = [cal components:NSWeekCalendarUnit
                                             fromDate:earliestDate
                                               toDate:dateToCheck
                                              options:0];
    NSUInteger weekNumber           = components.week;
    
    // Lookup the entry in the strings array.
    NSString *string;
    if (weekNumber < [strings count])
    {
        string = [strings objectAtIndex:weekNumber];
    }
    
    // Output is:  "String is: 20122301"
    NSLog(@"String is: %@", string);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Unsure of the best way to describe this, but I need to calculate the
Unsure how to get this string to work correctly. Keeps making my page blank.
Ok I was a little unsure on how best name this problem :) But
I'm new to rails and am unsure of the best way of doing this.
I'm unsure of the best solution for this but this is what I've done.
I am unsure of the best way to handle this. In my index view
I'm unsure on the best stack to build a chat application. Currently I'm thinking
I'm new to SQL and I'm a little unsure about the best way to
I am unsure what would be the best way to implement a success message
I'm a little unsure of how to ask this question, so I'll start as

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.