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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T07:37:18+00:00 2026-06-09T07:37:18+00:00

I’ve been using NSCalendar and NSDateComponents to return the difference (in days) between 2

  • 0

I’ve been using NSCalendar and NSDateComponents to return the difference (in days) between 2 dates. Here’s the code I’m using to get that.

NSCalendar *calendar = [NSCalendar currentCalendar];
calendar.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];

NSDate *todayDate = [NSDate date];
NSDateComponents *comps = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) 
                                 fromDate:todayDate];
[comps setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

NSDate *today = [calendar dateFromComponents:comps];

NSDate *startDate = self.date;
NSDate *endDate =  today;
NSLog(@"startDate: %@",startDate);
NSLog(@"endDate: %@",endDate);

NSDateComponents *components = [calendar components:NSDayCalendarUnit
                                            fromDate:endDate
                                              toDate:startDate
                                             options:0];
[components setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

NSInteger days = [components day];

The weird part is that this works perfectly during the early part of the day, but once I reach around 6 or 7PM (I’m using Mountain Time, which is GMT -7 I think), it stops working correctly. Instead of a returning the correct difference, it returns the difference minus 1 day. so For example, it returns -1 instead of 0, or 1 instead of 2.

I’m guessing this is a time zone issue. Does anyone know how to fix it? I’ve tried setting the timeZone for my NSCalendar and the NSDateComponents to GMT, local and system, but none seem to work.

Any ideas?

Thank you!

  • 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-09T07:37:20+00:00Added an answer on June 9, 2026 at 7:37 am

    It looks like the code is not handling the fractional difference between days. For example, if the start date is yesterday at 11:59 PM and the end date is today at 12:01 AM, the code will report the difference as 0 days. If you consider that to be a 1 day difference, then the code needs to handle that case.

    If you are selecting self.date with a date picker, self.date contains the date plus the current local time. At 6 or 7 PM your local time, it’s midnight GMT (i.e., tomorrow WRT your local time), which causes the unexpected -1 in your difference calculation.

    • 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
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
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
I am doing a simple coin flipping experiment for class that involves flipping a
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into

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.