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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:54:14+00:00 2026-05-27T21:54:14+00:00

The following code calculates the previous week number from a given week when passed

  • 0

The following code calculates the previous week number from a given week when passed year and week.

NSDateComponents *components = [[NSDateComponents alloc] init];
[components setYear:year];
[components setWeek:NSWeekCalendarUnit];
[components setWeekOfYear:(week-1)]; //Get the previous week
[components setWeekday:2]; //Monday
[components setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *date = [calendar dateFromComponents:components];

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyyww"];
NSString *newRevision = [formatter stringFromDate:date];

I was surprised that this handled cases like 201101 (which gives 201052), but it does not correctly handle years where there are 53 weeks. For example, for 201601 it returns 201552 instead of 201153. (At least it should return 201153 according to http://en.wikipedia.org/wiki/ISO_week_date anyway.)

Am I doing something wrong? I have traced through to make sure the inputs are correct.

  • 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-05-27T21:54:14+00:00Added an answer on May 27, 2026 at 9:54 pm

    We ended up working around the issue with a bit of hack. From the wikipedia link in the question, we got the list of all years with 53 weeks for the next 400 years and hard coded them in (with judicious comments and the ‘correct’ solution included).

    if (week == 1)
    {
        year = year - 1;
        week = 52;
        if (year == 2004 || year == 2009 || year == 2015 || year == 2020 || year == 2026 || year == 2032 || year == 2037 || year == 2043 || year == 2048 || year == 2054 || year == 2060 || year == 2065 || year == 2071 || year == 2076 || year == 2082 || year == 2088 || year == 2093 || year == 2099 || year == 2105 || year == 2111 || year == 2116 || year == 2122 || year == 2128 || year == 2133 || year == 2139 || year == 2144 || year == 2150 || year == 2156 || year == 2161 || year == 2167 || year == 2172 || year == 2178 || year == 2184 || year == 2189 || year == 2195 || year == 2201 || year == 2207 || year == 2212 || year == 2218 || year == 2224 || year == 2229 || year == 2235 || year == 2240 || year == 2246 || year == 2252 || year == 2257 || year == 2263 || year == 2268 || year == 2274 || year == 2280 || year == 2285 || year == 2291 || year == 2296 || year == 2303 || year == 2308 || year == 2314 || year == 2320 || year == 2325 || year == 2331 || year == 2336 || year == 2342 || year == 2348 || year == 2353 || year == 2359 || year == 2364 || year == 2370 || year == 2376 || year == 2381 || year == 2387 || year == 2392 || year == 2398)
        {
            week = 53;
        }
    }
    else
    {
        week = week-1;
    }
    

    It’s not ideal, but it will do the job until either Apple implements ISO 8601 or until the year 2404.

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

Sidebar

Related Questions

The following python code calculates the number of iterations to do stuff based on
The following code should find the appropriate project tag and remove it from the
Code The following code calculates the slope and intercept for a linear regression against
The following code has been created from a different post, but I now have
The following piece of code is from u-boot: /* Initialize GOT pointer. ** Global
I have the following code which calculates the median of a group of numbers
I have the following code in SQL (2005) which calculates the avarage user logins
suppose that there is given following matlab code function dp = derp(p) n =
The following is the source code of a program which calculates the area of
I created the following code to calculate the duration between two timestamps which can

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.