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 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 this code NSDateComponents *components = [[[NSDateComponents alloc] init] autorelease]; [components setYear:2011]; [components
I have this code: NSDateComponents *components = [[[NSDateComponents alloc] init] autorelease]; [components setYear:2011]; [components
My code is like below NSDateComponents *components = [[NSDateComponents alloc] init]; components.year = [aDay.year
I have the following code: NSDate *dateNow = [[NSDate alloc] init]; NSTimeInterval timeDifference =
I have: NSDate *d = [[NSDate alloc] init]; unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; [dateFormat setDateFormat:@yyyy-dd-MM HH:mm:ss]; NSDate *sss = [dateFormat dateFromString:cDate];
I'm using a NSDate that is incremented/decremented thanks to NSDateCompoments: dateComponents = [[NSDateComponents alloc]
For example, this method from NSCalendar takes a bitmask: - (NSDate *)dateByAddingComponents:(NSDateComponents *)comps toDate:(NSDate
I'm trying to set some components of todays date with NSDateComponent like so: NSDateComponents
I'm trying to create an NSDate from NSCalendar and NSDateComponents with: NSCalendar *calendar =

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.