如何获取从当前时间到 10:30 pm 每 30:00(30 分钟)的时间间隔
How to get time interval for every 30:00(30 minutes) from current time to 10:30 pm
需要帮助显示每 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));
此代码 returns 我当前时间和给定时间的差异我该如何进一步进行。
我会首先检查给定的开始时间是上午还是下午,之后,我会找到最近的(四舍五入)30 米的时间间隔,然后简单地加上 30 米,然后检查我是否在给定的停止时间,如果不增加计数器。
如果您想跟踪并return要打印的列表,您也可以将每个 30 米的间隔添加到列表中。
试试这个适合你的代码
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);
如果您得到的开始日期是当前日期和时间,那么结束日期会自动设置为 30 分钟使用 dateByAddingTimeInterval:
,请参阅我的示例,
NSDate *startDate = [NSDate date];
然后设置结束日期在代码下方,
currentdate = startDate; //today
endingDate = [startDate dateByAddingTimeInterval:(60*60)/2]; // 60*60 is 1 hour.
以上方法自动计算当前时间到结束时间加30分钟。
希望对您有所帮助
你可以这样做,
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);
}
在 for 循环中我采用了 numberOfIntervals*2
因为时间间隔是 30 min
,所以 60/30 = 2 而你的 datebyAddingThirtyMinute
是 1800 因为 30 分钟 = 1800 秒。如果你想要每 10 分钟后的时间,那么它应该是 60/10 = 6,所以它应该是 numberOfIntervals*6
。而你的 datebyAddingThirtyMinute
应该是 [fromTime dateByAddingTimeInterval:600];
希望这会有所帮助:)
最可靠的方法(还要考虑夏令时的变化)是使用 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);
需要帮助显示每 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));
此代码 returns 我当前时间和给定时间的差异我该如何进一步进行。
我会首先检查给定的开始时间是上午还是下午,之后,我会找到最近的(四舍五入)30 米的时间间隔,然后简单地加上 30 米,然后检查我是否在给定的停止时间,如果不增加计数器。
如果您想跟踪并return要打印的列表,您也可以将每个 30 米的间隔添加到列表中。
试试这个适合你的代码
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);
如果您得到的开始日期是当前日期和时间,那么结束日期会自动设置为 30 分钟使用 dateByAddingTimeInterval:
,请参阅我的示例,
NSDate *startDate = [NSDate date];
然后设置结束日期在代码下方,
currentdate = startDate; //today
endingDate = [startDate dateByAddingTimeInterval:(60*60)/2]; // 60*60 is 1 hour.
以上方法自动计算当前时间到结束时间加30分钟。
希望对您有所帮助
你可以这样做,
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);
}
在 for 循环中我采用了 numberOfIntervals*2
因为时间间隔是 30 min
,所以 60/30 = 2 而你的 datebyAddingThirtyMinute
是 1800 因为 30 分钟 = 1800 秒。如果你想要每 10 分钟后的时间,那么它应该是 60/10 = 6,所以它应该是 numberOfIntervals*6
。而你的 datebyAddingThirtyMinute
应该是 [fromTime dateByAddingTimeInterval:600];
希望这会有所帮助:)
最可靠的方法(还要考虑夏令时的变化)是使用 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);