Note that there are some explanatory texts on larger screens.

plurals
  1. POiPhone: method to calculate days till next birthday not accurate --?
    text
    copied!<p>Here's a puzzler. I use the following to calculate the number of days between today's date and an upcoming birthday:</p> <pre><code>-(int) daysTillBirthday: (NSDate*)aDate { // check to see if valid date was passed in //NSLog(@"aDate passed in is %@",aDate); if (aDate == nil) { //NSLog(@"aDate is NULL"); return -1; // return a negative so won't be picked in table } //** HOW MANY DAYS TO BDAY NSDate *birthDay = aDate; // [calendar dateFromComponents:myBirthDay]; //NSLog(@"birthDay: %@, today: %@",birthDay, [NSDate date]); NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease]; NSDateComponents *thisYearComponents = [calendar components:NSYearCalendarUnit fromDate:[NSDate date]]; NSDateComponents *birthDayComponents = [calendar components:NSMonthCalendarUnit|NSDayCalendarUnit fromDate:birthDay]; [birthDayComponents setYear:[thisYearComponents year]]; NSDate *birthDayThisYear = [calendar dateFromComponents:birthDayComponents]; //NSLog(@"birthDayThisYear: %@",birthDayThisYear); NSDateComponents *differenceHours = [calendar components:NSHourCalendarUnit fromDate:[NSDate date] toDate:birthDayThisYear options:0]; NSDateComponents *differenceDays = [calendar components:NSDayCalendarUnit fromDate:[NSDate date] toDate:birthDayThisYear options:0]; // NSLog(@"difference days: %i, hours %i",[differenceDays day],[differenceHours hour]); //*** I added this to try and correct the "error" *** if ([differenceDays day] == 0) { // is it today, or tomorrow? if (([differenceHours hour] &lt;= 0) &amp;&amp; ([differenceHours hour] &gt;= -24)) { // must be today //NSLog(@"TODAY"); return (0); [calendar release]; }else if (([differenceHours hour] &gt;= 0) &amp;&amp; ([differenceHours hour] &lt;= 24)) { //NSLog(@"TOMORROW"); return (1); [calendar release]; } } if ([differenceDays day] &lt; 0) { // this years birthday is already over. calculate distance to next years birthday [birthDayComponents setYear:[thisYearComponents year]+1]; birthDayThisYear = [calendar dateFromComponents:birthDayComponents]; differenceDays = [calendar components:NSDayCalendarUnit fromDate:[NSDate date] toDate:birthDayThisYear options:0]; } return ([differenceDays day]); [calendar release]; </code></pre> <p>} </p> <p>Everything works, but the results are not accurate! I often find that birthdays that are close to today, but one day apart, result in [differenceDays day] being the same! i.e. if today is 6/6/2011 and I have two birthdays, one on 6/7/2011 and another 6/8/2011, then they are both shown as 1 day away!</p> <p>Anyone have any better methods for accurately calculating this, or can spot the problem?</p> <p>Many thanks.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload