比较 NSDate 以检查今天是否在两者之间
感谢你们,我已经成功地将我的字符串数组转换为 NSDates。现在我试图检查今天的日期是否在我的两个日期(即 fromDate 和 toDate)之间。我看到了以下问题,但未能在我的代码中实现它们。太好了,我可以有任何其他方法或帮助使用用户提供的解决方案。
如何检查 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
}
thanks to you guys I have successfully converted my array of strings to NSDates. Now I'm trying to check if today's date falls in between my 2 dates (namely fromDate and toDate). I've seen the following questions but failed to implement them in my code. Be great I can have any other method or help on using the solutions given by the users.
NSDate between two given NSDates
How to Check if an NSDate occurs between two other NSDates
How can I check if an NSDate falls in between two other NSDates in an NSMutableArray
This is what I currently have:
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
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
执行以下操作,
NSTimeInterval 基本上是双精度类型,因此您可以比较如果 currTime 大于一个且小于另一个,则日期落在这两个 NSDate 之间。
Do the following,
NSTimeInterval is basically a double type so you can compare that if currTime is greater than one and less than the other than the date falls between those two NSDates.