Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to find hours between two NSDate objects?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 436
    Comment on it

    We can find the hours between two dates by using NSDateFormatter. NSDateFormatter provides formatting and conversion methods for NSDate objects. In below code, I have used formatter to convert the start date and end date into hours and minutes where "hh" stands for hours and "mm" stands for minutes.

    I have also used NSTimeInterval to get the time interval between two dates. Once we get the time interval then we convert it into numbers of hours by dividing it by 3600.

    Below code is used to show number of hours between two dates.

        NSString *startTime=@"2:30 AM";// time from
        NSString *endTime=@"8:40 AM"; //time to
        
        NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
        [formatter setDateFormat:@"hh:mm a"]; 
        NSDate *startDate = [formatter dateFromString:startTime];
        NSDate *endDate = [formatter dateFromString:endTime];
        NSLog(@"times are %@ %@",startDate ,endDate);
        NSTimeInterval timeBetween = [endDate timeIntervalSinceDate:startDate];// use to show time interval between time
        NSInteger noOfhours=timeBetween/3600; //converting into hours
        NSLog(@"number of hours  are %ld",(long)noOfhours);
    

     

 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: