如何获得从当前时间到晚上10:30每30:00(30分钟)的时间间隔

时间:2016-05-30 06:32:35

标签: ios objective-c time nsdate

需要帮助来显示每30分钟的时间间隔,假设current time is 11:45 am然后

时间间隔应为:12:00 pm,12:30 pm,01:00 pm,01:30 pm,02:00 pm,02:30 pm......10:30 pm.

 NSString *time = @"10.30 pm";

     NSDate *date1;
        NSDate *date2;
        {
            NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
            [formatter setDateFormat:@"hh.mm a"];
            date1 = [formatter dateFromString:time];
            date2 = [formatter dateFromString:[formatter stringFromDate:[NSDate date]]];

        }
        NSTimeInterval interval = [date1 timeIntervalSinceDate: date2];//[date1 timeIntervalSince1970] - [date2 timeIntervalSince1970];
        int hour = interval / 3600;
        int minute = (int)interval % 3600 / 60;

        NSLog(@"%@ %dh %dm", interval<0?@"-":@"+", ABS(hour), ABS(minute));

此代码返回当前时间和给定时间的差异如何进一步继续。

5 个答案:

答案 0 :(得分:4)

你可以做点什么,

  import itertools  
  merged = list(itertools.chain(*list2d))
  print [x for x in merged if not (x.isdigit() or x in '-+.')]

在for循环中我已经NSString *startTime = @"02:00 AM"; NSString *endTime = @"11:00 AM"; NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init]; [timeFormat setDateFormat:@"hh:mm a"]; NSDate* fromTime = [timeFormat dateFromString:startTime]; NSDate* toTime = [timeFormat dateFromString:endTime]; NSDate *dateByAddingThirtyMinute ; NSTimeInterval timeinterval = [toTime timeIntervalSinceDate:fromTime]; NSLog(@"time Int %f",timeinterval/3600); float numberOfIntervals = timeinterval/3600; NSLog(@"Start time %f",numberOfIntervals); for(int iCount = 0;iCount < numberOfIntervals*2 ;iCount ++) { dateByAddingThirtyMinute = [fromTime dateByAddingTimeInterval:1800]; fromTime = dateByAddingThirtyMinute; NSString *formattedDateString; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"hh:mm a"]; formattedDateString = [dateFormatter stringFromDate:dateByAddingThirtyMinute]; NSLog(@"Time after 30 min %@",formattedDateString); } ,因为时间间隔是numberOfIntervals*2,所以60/30 = 2而你的30 min是1800,因为30分钟= 1800秒。如果你想要每10分钟一次,那么它应该是60/10 = 6,所以应该datebyAddingThirtyMinute。您的numberOfIntervals*6应为datebyAddingThirtyMinute

希望这会有所帮助:)

答案 1 :(得分:2)

最可靠的方式(也考虑夏令时变化)是使用NSCalendar的日期数学功能。

根本不建议只使用dateByAddingTimeInterval添加秒数。

NSDate *now = [NSDate date];
NSCalendar *cal = [NSCalendar currentCalendar];
// get minute and hour from now
NSDateComponents *nowComponents = [cal components:NSCalendarUnitHour | NSCalendarUnitMinute fromDate: now];
NSDate *currentDate;
// if current minutes is not exactly 0 or 30 get back to the past half hour
if (nowComponents.minute % 30 != 0) {
   NSInteger pastHalfHourIndex = nowComponents.minute / 30;
   nowComponents.minute = pastHalfHourIndex * 30;
   currentDate = [cal nextDateAfterDate:now matchingHour: nowComponents.hour minute: nowComponents.minute second: 0 options: NSCalendarMatchNextTime | NSCalendarSearchBackwards];
} else {
   currentDate = now;
}    
NSMutableArray<NSDate *>* dates = [NSMutableArray array];
// loop and add 30 minutes until the end time (10:30 pm) is reached
while (nowComponents.minute != 30 || nowComponents.hour != 22) {
  currentDate = [cal dateByAddingUnit:NSCalendarUnitMinute value: 30 toDate: currentDate options: NSCalendarMatchNextTime];
  [dates addObject:currentDate];
  nowComponents = [cal components:NSCalendarUnitHour | NSCalendarUnitMinute fromDate: currentDate];
}
NSLog(@"%@", dates);

答案 2 :(得分:1)

尝试使用适用于您的代码

NSDateFormatter *datefrmt = [[NSDateFormatter alloc] init];
[datefrmt setDateFormat:@"hh:mm a"];
NSDate* startingTime = [datefrmt dateFromString:startTime];
NSDate* endingTime = [datefrmt dateFromString:endTime];
NSLog(@"Starting time is %@", startingTime);
NSLog(@"Stop time is %@", endingTime);    
NSDate * addingTimeInterval;    
addingTimeInterval = [startingTime dateByAddingTimeInterval:1800];    
NSLog(@"Required output is %@", addingTimeInterval);

答案 3 :(得分:0)

我首先检查给定的开始时间是上午还是下午,在这样做之后,我会找到最近的(向上舍入的)30米的时间间隔,然后简单地添加30米并检查我是否在给定的时间停止停止时间,如果没有增加计数器。

如果您想跟踪并返回要打印的列表,您还可以将每个30米间隔添加到列表中。

答案 4 :(得分:0)

如果您的开始日期是当前日期和时间,那么结束日期将自动设置为30分钟使用dateByAddingTimeInterval:,请参阅我的示例,

NSDate *startDate = [NSDate date];

然后设置结束日期低于代码,

currentdate = startDate; //today
endingDate = [startDate dateByAddingTimeInterval:(60*60)/2]; // 60*60 is 1 hour. 

以上方法自动计算当前时间到结束时间加30分钟。

希望它有用