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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T09:18:39+00:00 2026-05-19T09:18:39+00:00

How could I determine the number of days between two NSDate values (taking into

  • 0

How could I determine the number of days between two NSDate values (taking into consideration time as well)?

The NSDate values are in whatever form [NSDate date] takes.

Specifically, when a user enters the inactive state in my iPhone app, I store the following value:

exitDate = [NSDate date];

And when they open the app back up, I get the current time:

NSDate *now = [NSDate date];

Now I’d like to implement the following:

-(int)numberOfDaysBetweenStartDate:exitDate andEndDate:now
  • 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-19T09:18:40+00:00Added an answer on May 19, 2026 at 9:18 am

    Here’s an implementation I used to determine the number of calendar days between two dates:

    + (NSInteger)daysBetweenDate:(NSDate*)fromDateTime andDate:(NSDate*)toDateTime
    {
        NSDate *fromDate;
        NSDate *toDate;
    
        NSCalendar *calendar = [NSCalendar currentCalendar];
    
        [calendar rangeOfUnit:NSCalendarUnitDay startDate:&fromDate
            interval:NULL forDate:fromDateTime];
        [calendar rangeOfUnit:NSCalendarUnitDay startDate:&toDate
            interval:NULL forDate:toDateTime];
    
        NSDateComponents *difference = [calendar components:NSCalendarUnitDay
            fromDate:fromDate toDate:toDate options:0];
    
        return [difference day];
    }
    

    EDIT:

    Fantastic solution above, here’s Swift version below as an extension on NSDate:

    extension NSDate {
      func numberOfDaysUntilDateTime(toDateTime: NSDate, inTimeZone timeZone: NSTimeZone? = nil) -> Int {
        let calendar = NSCalendar.currentCalendar()
        if let timeZone = timeZone {
          calendar.timeZone = timeZone
        }
    
        var fromDate: NSDate?, toDate: NSDate?
    
        calendar.rangeOfUnit(.Day, startDate: &fromDate, interval: nil, forDate: self)
        calendar.rangeOfUnit(.Day, startDate: &toDate, interval: nil, forDate: toDateTime)
    
        let difference = calendar.components(.Day, fromDate: fromDate!, toDate: toDate!, options: [])
        return difference.day
      }
    }
    

    A bit of force unwrapping going on which you may want to remove depending on your use case.

    The above solution also works for time zones other than the current time zone, perfect for an app that shows information about places all around the world.

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

Sidebar

Related Questions

I have an application where two column could be populated from a number of
What is the best way to count the amount of time between two Calendar
I'm getting this Hibernate error: org.hibernate.MappingException: Could not determine type for: a.b.c.Results$BusinessDate, for columns:
What algorithms could i use to determine common characters in a set of strings?
I need to determine the current year in Java as an integer. I could
Could somebody please name a few. I could given time, but this is for
I need to determine the number of pages in a specified PDF file using
I need to calculate the number of days for a given month in python.
I'm trying to determine the number of digits in a c# ulong number, i'm
I'm having some trouble understanding the difference between these two code segments: I allocate

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.