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

时间:2022-12-08 01:08:01

Need help to show the time intervals for every 30 mins, suppose the current time is 11:45 am then

需要帮助显示每30分钟的时间间隔,假设当前时间是上午11:45

Time intervals should be : 12:00 pm,12:30 pm,01:00 pm,01:30 pm,02:00 pm,02:30 pm......10:30 pm.

时间间隔为:12:00 pm,12:30 pm,01:00 pm,01:30 pm,02:00 pm,02:30 pm10:30点。

 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));

This code returns me difference of current time and the given time how can I proceed further.

此代码返回当前时间的差值,以及给定的时间,我如何继续。

5 个解决方案

#1


4  

You can do something like,

你可以这样做,

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);
}

In for loop I have taken numberOfIntervals*2 because time interval is 30 min, so 60/30 = 2 and your datebyAddingThirtyMinute is 1800 because 30 min = 1800 seconds. If you want time after every 10 minutes then it should 60/10 = 6, so it should numberOfIntervals*6. And your datebyAddingThirtyMinute should be [fromTime dateByAddingTimeInterval:600];

在for循环中,我取了numberOfIntervals*2,因为时间间隔是30分钟,所以60/30 = 2,而你的日期是1800,因为30分钟= 1800秒。如果你想要每10分钟后的时间,那么应该是60/10 = 6,所以应该是numberOfIntervals*6。您的datebyAddingThirtyMinute应该是[fromTime dateByAddingTimeInterval:600];

Hope this will help :)

希望这能有所帮助:

#2


2  

The most reliable way (also to consider daylight saving time changes) is to use the date math capabilities of NSCalendar.

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

Simply adding seconds with dateByAddingTimeInterval is not recommended at all.

完全不建议使用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);

#3


1  

Try this code it works for you

试试这段代码对你有用。

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);

#4


0  

I would first check if the given start time is AM or PM, after doing that, I would find the nearest (rounded up) 30m interval of time and simply add 30m to it and check whether or not I was at the given stop stop time, and if not increment a counter.

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

You could also add each 30m interval to a list if you wanted to keep track of and return that list to be printed out.

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

#5


0  

If you get the Start date is current date and time, then the end date is automatically set 30 mins use dateByAddingTimeInterval:, see my example,

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

NSDate *startDate = [NSDate date];

then set ending date is below code,

然后设置结束日期在代码下面,

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

this above method automatically calculate current time to end time add 30 minutes.

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

hope its helpful

希望它有用

#1


4  

You can do something like,

你可以这样做,

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);
}

In for loop I have taken numberOfIntervals*2 because time interval is 30 min, so 60/30 = 2 and your datebyAddingThirtyMinute is 1800 because 30 min = 1800 seconds. If you want time after every 10 minutes then it should 60/10 = 6, so it should numberOfIntervals*6. And your datebyAddingThirtyMinute should be [fromTime dateByAddingTimeInterval:600];

在for循环中,我取了numberOfIntervals*2,因为时间间隔是30分钟,所以60/30 = 2,而你的日期是1800,因为30分钟= 1800秒。如果你想要每10分钟后的时间,那么应该是60/10 = 6,所以应该是numberOfIntervals*6。您的datebyAddingThirtyMinute应该是[fromTime dateByAddingTimeInterval:600];

Hope this will help :)

希望这能有所帮助:

#2


2  

The most reliable way (also to consider daylight saving time changes) is to use the date math capabilities of NSCalendar.

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

Simply adding seconds with dateByAddingTimeInterval is not recommended at all.

完全不建议使用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);

#3


1  

Try this code it works for you

试试这段代码对你有用。

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);

#4


0  

I would first check if the given start time is AM or PM, after doing that, I would find the nearest (rounded up) 30m interval of time and simply add 30m to it and check whether or not I was at the given stop stop time, and if not increment a counter.

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

You could also add each 30m interval to a list if you wanted to keep track of and return that list to be printed out.

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

#5


0  

If you get the Start date is current date and time, then the end date is automatically set 30 mins use dateByAddingTimeInterval:, see my example,

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

NSDate *startDate = [NSDate date];

then set ending date is below code,

然后设置结束日期在代码下面,

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

this above method automatically calculate current time to end time add 30 minutes.

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

hope its helpful

希望它有用