It is necessary to call this method and set only 2 dates, which we want to calculate in different ways.
-(void) calculateSleepHours:(NSDate *)sleepDate :(NSDate *)wakeStr { if (sleepDate !=nil && wakeStr!=nil) {`enter code here` NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init]; [dateFormatter1 setDateFormat:@"yyyy-MM-dd HH:mm:ssZ"]; NSDate *date1 = [ApplicationManager getInstance].sleepTime;; NSDate *date2 = [ApplicationManager getInstance].wakeupTime; NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSUInteger unitFlags = NSMonthCalendarUnit | NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit; NSDateComponents *components = [gregorian components:unitFlags fromDate:date1 toDate:date2 options:0]; NSInteger months = [components month]; NSInteger days = [components day]; NSInteger hours = [components hour]; NSInteger minute=[components minute]; NSInteger second=[components second]; DLog(@"Month %ld day %ld hour is %ld min %ld sec %ld ",(long)months,(long)days,(long)hours,(long)minute,(long)second); sleepHours.text=[NSString stringWithFormat:@"Hour %ld Min %ld Sec %ld",(long)hours,(long)minute,(long)second]; } }
Chandni - Systematix Mar 06 '14 at 13:12 2014-03-06 13:12
source share