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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T15:56:54+00:00 2026-06-01T15:56:54+00:00

I’m working on a project that needs to check the time difference from a

  • 0

I’m working on a project that needs to check the time difference from a first launch date. I’m using the NSDayCalendarUnit and NSWeekCalendarUnit. Basically for the first 2 weeks on every second day I want to perform something. The object I’m using needs to be in a certain state for each two days at a time.

So for example

Day 1 & 2, Week 0 - State 1
Day 3 & 4, Week 0 - State 2
...
Day 1 & 2, Week 1 - State 8
Day 3 & 4, Week 1 - State 9

Here is my code:

    // get the data/time difference from the first launch
    int daysDifferent = [[dateDifferenceInfo objectForKey:@"days"] intValue];
    int weeksDifferent = [[dateDifferenceInfo objectForKey:@"weeks"] intValue];

    if(daysDifferent == 2 | daysDifferent == 3 && !weeksDifferent && _dot.age != 2){
        // set state
    }

    if(daysDifferent == 4 | daysDifferent == 5 && !weeksDifferent && _dot.age != 3){
        // set state
    }
    if((daysDifferent == 6 && weeksDifferent == 0 | daysDifferent == 0 && weeksDifferent == 1) && _dot.age != 4){
       // set state
    }

    if(daysDifferent == 0 | daysDifferent == 1 && weeksDifferent == 1 && _dot.age != 5){
       // set state
    }

    if(daysDifferent == 2 | daysDifferent == 3 && weeksDifferent == 1  && _dot.age != 6){
      // set state   
    }

    if(daysDifferent == 4| daysDifferent == 5 && weeksDifferent == 1  && _dot.age != 7){
      // set state   
    }

    if((daysDifferent == 6 && weeksDifferent == 1 | daysDifferent == 0 && weeksDifferent == 2)  && _dot.age != 8){
      // set state
    }

    if(weeksDifferent >= 2 && !(daysDifferent % 2)){
      // set state
    }

Side note: I know it’s bad code, I plan to replace this with switch cases, I just need to logic sorted first.

My question is is there a better way of calculating this kind of pattern?

  • 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-01T15:56:55+00:00Added an answer on June 1, 2026 at 3:56 pm

    I’m not totally clear on your goal, as you say:

    for the first 2 weeks on every second day I want to perform something.

    So for example

    Day 1 & 2, Week 0 - State 1
    Day 3 & 4, Week 0 - State 2
    ...
    Day 1 & 2, Week 1 - State 8
    Day 3 & 4, Week 1 - State 9
    

    Where I would have thought it would be

    Day 1 & 2, Week 0 - State 1
    Day 3 & 4, Week 0 - State 2
    Day 5 & 6, Week 0 - State 3
    Day 7 Week 0 & Day 1 Week 1 - State 4.
    ...
    

    So if your example is right, the calculation is different.

    Based on the simpler example, I would calculate the time interval since the first date, and divide by twice the length of a day, taking the floor. That is the state:

    NSDate *firstDate = ...
    NSDate *secondDate = ...
    NSTimeInterval interval = [ secondDate timeIntervalSinceDate: firstDate ];
    double secondsPerDay = 60 * 60 * 24.0; // s per min times min per hr times hrs per dy. 
    int state = (int) (interval / secondsPerDay * 0.5 ) + 1; 
    

    If, on the other hand, it really is as you have described, the approach I would use as fast and simple would be to do a similar calculation – leaving out the 0.5 factor in the last line, and then use a simple table lookup to get the state from the day:

    int table[ 14 ] = { 1, 1, 2, 2, 3, 3, 4, 5, 5, 6, 6, 7, 7, 8 };
    int state = table[ (int) (interval / secondsPerDay ) ]; 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I'm making a simple page using Google Maps API 3. My first. One marker
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of 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.