Objective C - 生成时间间隔为 X 分钟的时间对象数组?
Objective C - Generate an array of time objects with an interval of X mins?
我想创建一个 24 小时制时间对象数组,每个对象之间的间隔为 X 分钟;并且应该验证为 NSDate/NSTime 个对象。
即:生成的输出
00:00, 00:30, 01:00, 01:30, etc
我希望时间对象可以针对 NSDates 进行验证。
我在想我们有一组 NSDates,但我不确定如何在不超过 24 小时的情况下使每个 NSDates 与前一个相差 X 分钟。
非常感谢
编辑:第一次尝试
好的,我已经尝试了下面给出的答案
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:[NSDate date]];
components.timeZone = [NSTimeZone localTimeZone];
components.hour = 11;
components.minute = 0;
components.second = 0;
NSDate *date = [[NSCalendar currentCalendar] dateFromComponents:components];
NSDateFormatter *timeFormatter = [NSDateFormatter vic_timeFormatter];
[timeFormatter setTimeZone:[NSTimeZone localTimeZone]];
for (int i=1; i<48; i++) {
NSUInteger seconds = (1800 * i);
NSDate *newDate= [NSDate dateWithTimeInterval:seconds sinceDate:date];
NSLog(@"newDate = %@", newDate);
NSString *formatted = [timeFormatter stringFromDate:newDate];
NSLog(@"Formatted = %@", formatted);
}
timeFormatter=nil;
我选择了 48,因为 1800 在 24 小时制中出现了 48 次
除了在我的日志中,它总是在10:30
左右结束
理想情况下,我希望 运行 直到 11:30PM 或 12:00PM;不管时间从什么时候开始(在上面的场景中,它从上午 11 点开始)
在下方记录 returns;
newDate = 2015-02-20 11:30:00 +0000
Formatted = 11:30 AM
newDate = 2015-02-20 12:00:00 +0000
Formatted = 12:00 PM
newDate = 2015-02-20 12:30:00 +0000
Formatted = 12:30 PM
newDate = 2015-02-20 13:00:00 +0000
Formatted = 1:00 PM
newDate = 2015-02-20 13:30:00 +0000
Formatted = 1:30 PM
newDate = 2015-02-20 14:00:00 +0000
Formatted = 2:00 PM
newDate = 2015-02-20 14:30:00 +0000
Formatted = 2:30 PM
newDate = 2015-02-20 15:00:00 +0000
Formatted = 3:00 PM
newDate = 2015-02-20 15:30:00 +0000
Formatted = 3:30 PM
newDate = 2015-02-20 16:00:00 +0000
Formatted = 4:00 PM
newDate = 2015-02-20 16:30:00 +0000
Formatted = 4:30 PM
newDate = 2015-02-20 17:00:00 +0000
Formatted = 5:00 PM
newDate = 2015-02-20 17:30:00 +0000
Formatted = 5:30 PM
newDate = 2015-02-20 18:00:00 +0000
Formatted = 6:00 PM
newDate = 2015-02-20 18:30:00 +0000
Formatted = 6:30 PM
newDate = 2015-02-20 19:00:00 +0000
Formatted = 7:00 PM
newDate = 2015-02-20 19:30:00 +0000
Formatted = 7:30 PM
newDate = 2015-02-20 20:00:00 +0000
Formatted = 8:00 PM
newDate = 2015-02-20 20:30:00 +0000
Formatted = 8:30 PM
newDate = 2015-02-20 21:00:00 +0000
Formatted = 9:00 PM
newDate = 2015-02-20 21:30:00 +0000
Formatted = 9:30 PM
newDate = 2015-02-20 22:00:00 +0000
Formatted = 10:00 PM
newDate = 2015-02-20 22:30:00 +0000
Formatted = 10:30 PM
newDate = 2015-02-20 23:00:00 +0000
Formatted = 11:00 PM
newDate = 2015-02-20 23:30:00 +0000
Formatted = 11:30 PM
newDate = 2015-02-21 00:00:00 +0000
Formatted = 12:00 AM
newDate = 2015-02-21 00:30:00 +0000
Formatted = 12:30 AM
newDate = 2015-02-21 01:00:00 +0000
Formatted = 1:00 AM
newDate = 2015-02-21 01:30:00 +0000
Formatted = 1:30 AM
newDate = 2015-02-21 02:00:00 +0000
Formatted = 2:00 AM
newDate = 2015-02-21 02:30:00 +0000
Formatted = 2:30 AM
newDate = 2015-02-21 03:00:00 +0000
Formatted = 3:00 AM
newDate = 2015-02-21 03:30:00 +0000
Formatted = 3:30 AM
newDate = 2015-02-21 04:00:00 +0000
Formatted = 4:00 AM
newDate = 2015-02-21 04:30:00 +0000
Formatted = 4:30 AM
newDate = 2015-02-21 05:00:00 +0000
Formatted = 5:00 AM
newDate = 2015-02-21 05:30:00 +0000
Formatted = 5:30 AM
newDate = 2015-02-21 06:00:00 +0000
Formatted = 6:00 AM
newDate = 2015-02-21 06:30:00 +0000
Formatted = 6:30 AM
newDate = 2015-02-21 07:00:00 +0000
Formatted = 7:00 AM
newDate = 2015-02-21 07:30:00 +0000
Formatted = 7:30 AM
newDate = 2015-02-21 08:00:00 +0000
Formatted = 8:00 AM
newDate = 2015-02-21 08:30:00 +0000
Formatted = 8:30 AM
newDate = 2015-02-21 09:00:00 +0000
Formatted = 9:00 AM
newDate = 2015-02-21 09:30:00 +0000
Formatted = 9:30 AM
newDate = 2015-02-21 10:00:00 +0000
Formatted = 10:00 AM
newDate = 2015-02-21 10:30:00 +0000
Formatted = 10:30 AM
尝试使用 dateWithTimeInterval 方法。例子
NSDate *newDate= [NSDate dateWithTimeInterval:1800 sinceDate:yourDate];
我想创建一个 24 小时制时间对象数组,每个对象之间的间隔为 X 分钟;并且应该验证为 NSDate/NSTime 个对象。
即:生成的输出
00:00, 00:30, 01:00, 01:30, etc
我希望时间对象可以针对 NSDates 进行验证。
我在想我们有一组 NSDates,但我不确定如何在不超过 24 小时的情况下使每个 NSDates 与前一个相差 X 分钟。
非常感谢
编辑:第一次尝试
好的,我已经尝试了下面给出的答案
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:[NSDate date]];
components.timeZone = [NSTimeZone localTimeZone];
components.hour = 11;
components.minute = 0;
components.second = 0;
NSDate *date = [[NSCalendar currentCalendar] dateFromComponents:components];
NSDateFormatter *timeFormatter = [NSDateFormatter vic_timeFormatter];
[timeFormatter setTimeZone:[NSTimeZone localTimeZone]];
for (int i=1; i<48; i++) {
NSUInteger seconds = (1800 * i);
NSDate *newDate= [NSDate dateWithTimeInterval:seconds sinceDate:date];
NSLog(@"newDate = %@", newDate);
NSString *formatted = [timeFormatter stringFromDate:newDate];
NSLog(@"Formatted = %@", formatted);
}
timeFormatter=nil;
我选择了 48,因为 1800 在 24 小时制中出现了 48 次
除了在我的日志中,它总是在10:30
左右结束理想情况下,我希望 运行 直到 11:30PM 或 12:00PM;不管时间从什么时候开始(在上面的场景中,它从上午 11 点开始)
在下方记录 returns;
newDate = 2015-02-20 11:30:00 +0000
Formatted = 11:30 AM
newDate = 2015-02-20 12:00:00 +0000
Formatted = 12:00 PM
newDate = 2015-02-20 12:30:00 +0000
Formatted = 12:30 PM
newDate = 2015-02-20 13:00:00 +0000
Formatted = 1:00 PM
newDate = 2015-02-20 13:30:00 +0000
Formatted = 1:30 PM
newDate = 2015-02-20 14:00:00 +0000
Formatted = 2:00 PM
newDate = 2015-02-20 14:30:00 +0000
Formatted = 2:30 PM
newDate = 2015-02-20 15:00:00 +0000
Formatted = 3:00 PM
newDate = 2015-02-20 15:30:00 +0000
Formatted = 3:30 PM
newDate = 2015-02-20 16:00:00 +0000
Formatted = 4:00 PM
newDate = 2015-02-20 16:30:00 +0000
Formatted = 4:30 PM
newDate = 2015-02-20 17:00:00 +0000
Formatted = 5:00 PM
newDate = 2015-02-20 17:30:00 +0000
Formatted = 5:30 PM
newDate = 2015-02-20 18:00:00 +0000
Formatted = 6:00 PM
newDate = 2015-02-20 18:30:00 +0000
Formatted = 6:30 PM
newDate = 2015-02-20 19:00:00 +0000
Formatted = 7:00 PM
newDate = 2015-02-20 19:30:00 +0000
Formatted = 7:30 PM
newDate = 2015-02-20 20:00:00 +0000
Formatted = 8:00 PM
newDate = 2015-02-20 20:30:00 +0000
Formatted = 8:30 PM
newDate = 2015-02-20 21:00:00 +0000
Formatted = 9:00 PM
newDate = 2015-02-20 21:30:00 +0000
Formatted = 9:30 PM
newDate = 2015-02-20 22:00:00 +0000
Formatted = 10:00 PM
newDate = 2015-02-20 22:30:00 +0000
Formatted = 10:30 PM
newDate = 2015-02-20 23:00:00 +0000
Formatted = 11:00 PM
newDate = 2015-02-20 23:30:00 +0000
Formatted = 11:30 PM
newDate = 2015-02-21 00:00:00 +0000
Formatted = 12:00 AM
newDate = 2015-02-21 00:30:00 +0000
Formatted = 12:30 AM
newDate = 2015-02-21 01:00:00 +0000
Formatted = 1:00 AM
newDate = 2015-02-21 01:30:00 +0000
Formatted = 1:30 AM
newDate = 2015-02-21 02:00:00 +0000
Formatted = 2:00 AM
newDate = 2015-02-21 02:30:00 +0000
Formatted = 2:30 AM
newDate = 2015-02-21 03:00:00 +0000
Formatted = 3:00 AM
newDate = 2015-02-21 03:30:00 +0000
Formatted = 3:30 AM
newDate = 2015-02-21 04:00:00 +0000
Formatted = 4:00 AM
newDate = 2015-02-21 04:30:00 +0000
Formatted = 4:30 AM
newDate = 2015-02-21 05:00:00 +0000
Formatted = 5:00 AM
newDate = 2015-02-21 05:30:00 +0000
Formatted = 5:30 AM
newDate = 2015-02-21 06:00:00 +0000
Formatted = 6:00 AM
newDate = 2015-02-21 06:30:00 +0000
Formatted = 6:30 AM
newDate = 2015-02-21 07:00:00 +0000
Formatted = 7:00 AM
newDate = 2015-02-21 07:30:00 +0000
Formatted = 7:30 AM
newDate = 2015-02-21 08:00:00 +0000
Formatted = 8:00 AM
newDate = 2015-02-21 08:30:00 +0000
Formatted = 8:30 AM
newDate = 2015-02-21 09:00:00 +0000
Formatted = 9:00 AM
newDate = 2015-02-21 09:30:00 +0000
Formatted = 9:30 AM
newDate = 2015-02-21 10:00:00 +0000
Formatted = 10:00 AM
newDate = 2015-02-21 10:30:00 +0000
Formatted = 10:30 AM
尝试使用 dateWithTimeInterval 方法。例子
NSDate *newDate= [NSDate dateWithTimeInterval:1800 sinceDate:yourDate];