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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T14:14:12+00:00 2026-05-20T14:14:12+00:00

Basically I want to know I a user chooses from my settings what day

  • 0

Basically I want to know I a user chooses from my settings what day they want a week to start on (This would have a value in the format, @”Mon” for example) and I have an NSMutableArray that holds NSDates (users add theses dates throughout the life of the app) ordered in a chronologically ascending manner, but not necessarily consecutive (there could be day’s that users missed and did not add the date to the array), how could I determine that a particular NSDate in the array is part of a different week (relative to the date the user chose for a week to start on)?

  • 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-20T14:14:12+00:00Added an answer on May 20, 2026 at 2:14 pm

    This will give you the difference between two dates in weeks:

    NSDateComponents *dateDifference = [gregorian components:NSWeekCalendarUnit fromDate:day1 toDate:day2 options:0];
    NSUInteger weeksDiff = [dateDifference week];
    

    So if(!weekDiff){ /*same week*/}

    Complete example:

    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    
    NSDateComponents *comps1 = [[NSDateComponents alloc] init];
    NSDateComponents *comps2 = [[NSDateComponents alloc] init];
    [comps1 setDay:5];
    [comps2 setDay:12];
    
    NSDate *day1 = [gregorian dateByAddingComponents:comps1 toDate:[NSDate date] options:0];
    NSDate *day2 = [gregorian dateByAddingComponents:comps2 toDate:[NSDate date] options:0];
    
    
    NSDateComponents *dateDifference = [gregorian components:NSWeekCalendarUnit fromDate:day1 toDate:day2 options:0];
    
    NSLog(@"\n%@ \n%@\n%d", day1, day2, [dateDifference week]);
    [comps1 release];
    [comps2 release];
    [gregorian release];
    

    For converting strings to NSDates, see NSDateFormatter

    edit as response to comment
    if you want to have a week that starts with the first date you pass in, you could instead count the days and check if they have more than 7 days difference.

    NSDateComponents *dateDifference = [gregorian components:NSDayCalendarUnit fromDate:day1 toDate:day2 options:0];
    NSUInteger daysDiff = [dateDifference day];
    if(weekDiff < 7){ /*same week*/}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I basically want to autodetect a user's timezone using Rails. I can use this
basically I want to encapsulate a simple component from code that I already have.
I basically want to know what the system's input language is currently on (for
Basically I want to know how to set center alignment for a cell using
Basically, I want to know what is best to avoid future problems and confusions
Basically, I just want to know if its possible to use Nhibernate to migrate
Bascially I want to know the best way to hide/show an ASP.NET control from
I basically want to do this: grep 'example.com' www_log > example.com.YYYY-MM-DD-H:i:S.log ...with of course
I basically want to do this in code: PersonList myPersonList; //populate myPersonList here, not
Basically I want to know if a given nodes children are visible in a

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.