Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think I have found a solution. Checking the output carefully, it appears to all come down to the difference in HOURS. For example: comparing today with tomorrow's date might end up being, say, 18 hours away. This results in [difference day] being set at 0 i.e. it thinks tomorrow is today because it is less than 24 hours away. </p> <p>You can see the fix below. I take the number of hours e.g. 18 and divide by 24 (to get the number of days). In this case 18/24 = 0.75. I then round this up i.e. to "1." So while [difference days] thinks tomorrow is today, by rounding up the hours, you know it is in fact tomorrow.</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]; NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease]; NSDateComponents *thisYearComponents = [calendar components:NSYearCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit fromDate:[NSDate date]]; NSDateComponents *birthDayComponents = [calendar components:NSMonthCalendarUnit|NSDayCalendarUnit fromDate:birthDay]; NSInteger timeNow = [thisYearComponents hour]; [birthDayComponents setYear:[thisYearComponents year]]; [birthDayComponents setHour:timeNow]; NSDate *birthDayThisYear = [calendar dateFromComponents:birthDayComponents]; //NSLog(@"today %@, birthday %@",[NSDate date],birthDayThisYear); NSDateComponents *difference = [calendar components:NSDayCalendarUnit fromDate:[NSDate date] toDate:birthDayThisYear options:0]; NSDateComponents *differenceHours = [calendar components:NSHourCalendarUnit fromDate:[NSDate date] toDate:birthDayThisYear options:0]; double daysFromHours = ((double)[differenceHours hour])/24; // calculate number of days from hours (and round up) int roundedDaysFromHours = ceil(daysFromHours); NSLog(@"daysFromHours %.02f, roundedDaysFromHours %i",daysFromHours,roundedDaysFromHours); if ([difference day] &lt; 0) { // this years birthday is already over. calculate distance to next years birthday [birthDayComponents setYear:[thisYearComponents year]+1]; birthDayThisYear = [calendar dateFromComponents:birthDayComponents]; difference = [calendar components:NSDayCalendarUnit fromDate:[NSDate date] toDate:birthDayThisYear options:0]; } //NSLog(@"%i days until birthday", [difference day]); return (roundedDaysFromHours); [calendar release]; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

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