Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Try following code for get number of days, min, hrs, months between two dates.</p> <pre><code>// The time interval NSTimeInterval theTimeInterval = 326.4; // Get the system calendar NSCalendar *sysCalendar = [NSCalendar currentCalendar]; // Create the NSDates NSDate *date1 = [[NSDate alloc] init]; NSDate *date2 = [[NSDate alloc] initWithTimeInterval:theTimeInterval sinceDate:date1]; // Get conversion to months, days, hours, minutes unsigned int unitFlags = NSHourCalendarUnit | NSMinuteCalendarUnit | NSDayCalendarUnit | NSMonthCalendarUnit; NSDateComponents *conversionInfo = [sysCalendar components:unitFlags fromDate:date1 toDate:date2 options:0]; NSLog(@"Conversion: %dmin %dhours %ddays %dmoths",[conversionInfo minute], [conversionInfo hour], [conversionInfo day], [conversionInfo month]); [date1 release]; [date2 release]; </code></pre> <p>Try following code for get number of days between two dates.</p> <pre><code>-(int)howManyDaysHavePast:(NSDate*)lastDate today:(NSDate*)today { NSDate *startDate = lastDate; NSDate *endDate = today; NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; unsigned int unitFlags = NSDayCalendarUnit; NSDateComponents *components = [gregorian components:unitFlags fromDate:startDate toDate:endDate options:0]; int days = [components day]; return days; // multiply by 24 for get remain hrs. } </code></pre> <p>Or fololw </p> <ul> <li><p><a href="https://stackoverflow.com/questions/5866852/how-can-i-get-the-date-difference">How can i get the Date Difference?</a></p></li> <li><p><a href="https://stackoverflow.com/questions/1189252/how-to-convert-an-nstimeinterval-seconds-into-minutes">How to convert an NSTimeInterval (seconds) into minutes</a></p></li> </ul>
 

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