感谢你们,我成功地将我的字符串数组转换为NSDates。现在我正在尝试检查今天的日期是否在我的 2 个日期(即 fromDate 和 toDate)之间。我看到了以下问题,但未能在我的代码中实现它们。很棒,我可以有任何其他方法或帮助使用用户给出的解决方案。
在两个给定的 NSD 之间生成
如何检查 NSDate 是否发生在其他两个 NSDate 之间
如何检查 NSDate 是否位于 NSMutableArray 中的另外两个 NSDate 之间
这是我目前拥有的:
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:@"yyyy-MM-dd"];
NSTimeZone *tz = [NSTimeZone localTimeZone];
[format setTimeZone:tz];
NSDate *now = [[NSDate alloc] init];
NSString *todaysDate = [format stringFromDate:now];
NSLog(@"todaysDate: %@", todaysDate);
//converting string - date
int size = [appDelegate.ALLevents count];
for (NSUInteger i = 0; i < size; i++) {
Event *aEvent = [appDelegate.ALLevents objectAtIndex:i];
//Convert fromDate
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MM-yyyy"];
NSDate *fromDate = [dateFormatter dateFromString:aEvent.fromDate];
[dateFormatter setTimeZone:tz];
NSLog(@"fromDate: %@", fromDate);
//Convert toDate
NSDate *toDate = [dateFormatter dateFromString:aEvent.toDate];
NSLog(@"toDate: %@", toDate);
[dateFormatter release];
//Compare if today falls in between
//codes here
}
执行以下操作,
NSTimeInterval fromTime = [fromDate timeIntervalSinceReferenceDate];
NSTimeInterval toTime = [toDate timeIntervalSinceReferenceDate];
NSTimeInterval currTime = [[NSDate date] timeIntervalSinceReferenceDate];
NSTimeInterval 基本上是一个双精度类型,因此您可以比较如果 currTime 大于 1 且小于另一个,则日期介于这两个 NSDate 之间。