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)