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

  • Home
  • SEARCH
  • 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 4620676
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:35:34+00:00 2026-05-22T02:35:34+00:00

NSDateComponents *components = [[[NSDateComponents alloc] init] autorelease]; [components setTimeZone:[ NSTimeZone timeZoneForSecondsFromGMT:(+0*3600) ] ] ;

  • 0
NSDateComponents *components = [[[NSDateComponents alloc] init] autorelease];
[components setTimeZone:[ NSTimeZone timeZoneForSecondsFromGMT:(+0*3600) ] ] ;
[components setYear:2011];
[components setDay:13];
[components setMonth:5];
NSDate *date1 = [gregorianCalendar dateFromComponents:components];
NSDate *date2 = [[NSDate alloc] init];


    NSTimeInterval diff = [data2 timeIntervalSinceDate:date1];
    NSString *intervalString = [NSString stringWithFormat:@"%f", diff];
    int second = [intervalString intValue];
    int period = second/3600/24; 
    NSLog(@"period:%d", period);
    NSLog(@"date1:%@", data1);
    NSLog(@"date2:%@", data2);

In consol the result is:

2011-05-12 10:57:00.406 Project[297:707] period:0;

2011-05-12 10:57:00.375 Project[297:707] data2:2011-05-12 08:56:52 +0000

2011-05-12 10:57:00.402 Project[297:707] data1:2011-05-13 00:00:00 +0000

I don’t understand why period is “0”, it must be “1”; Can you help me?

  • 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-22T02:35:34+00:00Added an answer on May 22, 2026 at 2:35 am

    NSTimeInterval is just a double, so you shouldn’t need to convert it to a string then back to an int.

    What happens if you do something like this:

    NSTimeInterval diff = [data2 timeIntervalSinceDate:date1];
    int period = (int)diff/3600/24; 
    NSLog(@"period:%d", period);
    

    Also if the interval is diff is less than 3600*24 the result of diff/3600/24 will be less than 1, so the int value will be flattened to 0.

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

Sidebar

Related Questions

I have: NSDate *d = [[NSDate alloc] init]; unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit
I have the following code: NSDate *dateNow = [[NSDate alloc] init]; NSTimeInterval timeDifference =
I'm trying to set some components of todays date with NSDateComponent like so: NSDateComponents
i use this code to get informations from strings with this format 01-05-2011 NSDateFormatter
I am having problems with adding values to dates and also getting differences between
Let's say we are the 15/07/2010, I would like to get the first day
I have a time of NSString type. i wonder get the day, but i
I have a little project that is a timer that counts down 'til midnight,
In my application I'm using following codes to retrieve current date and day :-
For some reason, the tableView: titleForHeaderInSection: method is causing the application to crash without

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.