2

So being the newbie, I have looked through Stackoverflow regarding this question and haven't found an example similar to what I am looking for. Many of the examples care about leap year/seconds. I don't. I just need a number between 0 and 90. I am not concerned about half days and so forth. Example: 1 Jan 2012@2359 - 2 Jan 2012@0001 = one full day in my counter.

What I need is to set a date and as time goes on, have a count down showing 'originalDate + 90 - Today's date'. Once 'originalDate + 90 - Today's date' = 0, just show 0 from then on.

How do I do that?

Thanks.

sdknewbie
  • 659
  • 2
  • 9
  • 13
  • 1
    you can refer -http://stackoverflow.com/questions/4739483/number-of-days-between-two-nsdates, here also number of days are calculated only. – rishi Jan 16 '12 at 16:41

2 Answers2

2

Some things to look into:

NSDate has a method called timeIntervalSinceDate. Use this to get the number of seconds between the receiver and a given date.

Use NSDateComponents to set up your "90 days from now", so you can configure the number of seconds and all that. The Apple documentation describes this all in detail.

The basic steps you'll take:

  • Store "now" as an NSDate object.
  • Create an NSDate object for 90 days from now.
  • Compare the two, storing the result in an NSInteger or double, as needed.
  • Do something with that result. (Check if it's greater than zero, for example.)
Moshe
  • 57,511
  • 78
  • 272
  • 425
  • You got me thinking in the right direction & I found https://github.com/erica/NSDate-Extensions - Thanks for the push! NSDate *expireDate = [[NSUserDefaults standardUserDefaults] objectForKey:@"ExpireDate"]; if (expireDate == nil) { expireDate = [NSDate date]; } NSDate *now = [NSDate date]; NSInteger count = [now daysBeforeDate:expireDate]; if (count <= 0) { counter.text = @"0"; } else { NSString *strCount = [NSString stringWithFormat:@"%i", count]; counter.text = strCount; } – sdknewbie Jan 27 '12 at 15:07
0

First get your enddate from your start date

NSTimeInterval ninetyDays = 90*24*60;
NSDate endDate = [NSDate dateWithTimeInterval:ninetyDays sinceDate:originalDate];

then remove the "time" (hours minutes, seconds) portion of your NSDate objects:

NSDate *currentDateInDaysPrecision;
[calendar rangeOfUnit:NSDayCalendarUnit startDate:&currentDateInDaysPrecision
             interval:NULL forDate:[NSDate date]];

NSDate *endDateInDaysPrecision;
[calendar rangeOfUnit:NSDayCalendarUnit startDate:&endDateInDaysPrecision
             interval:NULL forDate:endDate];

Now you can use that to calculate the difference in days:

    // calculate the difference in days
NSDateComponents *difference = [calendar components:NSDayCalendarUnit
                                           fromDate:currentDateInDaysPrecision
                                             toDate:endDateInDaysPrecision
                                            options:0];
NSInteger differenceInDays = [difference day];

And that should do it.

edit:

I learned this from another SO answer

Community
  • 1
  • 1
João Portela
  • 6,338
  • 7
  • 38
  • 51