Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to get time intervals in hours between start time and end time in objective C

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.23k
    Comment on it

    If we want to get the time intervals between the start time and end time then use the following code-

    it will return the array of hours according to the start time and end time.

    + (NSMutableArray *)getTimeIntervals:(NSString *)startTime andEndTime:(NSString *)endTime {
        NSMutableArray *hoursArray=[[NSMutableArray alloc]init];
        NSDateFormatter *dateFormatter1=[[NSDateFormatter alloc]init];
        [dateFormatter1 setDateFormat:@"hh:mm a"];
        NSDate *startString = [dateFormatter1 dateFromString:startTime];
        NSDate *endString = [dateFormatter1 dateFromString:endTime];
        
        NSTimeInterval interval = [endString timeIntervalSinceDate:startString];
        NSInteger t1=(NSInteger)interval;
        NSInteger hours=t1/3600;
        
    
        for (int i=0;i<=hours; i++)
       {
            NSString *meridian;
            NSDate *plusOneHour ;
           if(i==0)
               plusOneHour=startString;
    
           else
               plusOneHour = [startString dateByAddingTimeInterval:60.0f * 60.0f];
           
            NSCalendar *calendar = [NSCalendar currentCalendar];
            NSDateComponents *components = [calendar components:(NSCalendarUnitHour | NSCalendarUnitMinute) fromDate:plusOneHour];
            NSInteger hour = [components hour];
            meridian=hour>=12?@"PM":@"AM";
            if(hour>12){
               hour=hour-12;
            }
           
            NSString *string = [NSString stringWithFormat:@"%ld %@", (long)hour,meridian];
            startString=plusOneHour;
            [hoursArray addObject:string];
        }
    
        
            return hoursArray;
    }
    

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: