比较 NSDate 以检查今天是否在两者之间

发布于 2024-11-06 00:46:55 字数 1640 浏览 0 评论 0原文

感谢你们,我已经成功地将我的字符串数组转换为 NSDates。现在我试图检查今天的日期是否在我的两个日期(即 fromDate 和 toDate)之间。我看到了以下问题,但未能在我的代码中实现它们。太好了,我可以有任何其他方法或帮助使用用户提供的解决方案。

两个给定 NSDate 之间的 NSDate

如何检查 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一绘本一梦想 2024-11-13 00:46:55

执行以下操作,

NSTimeInterval fromTime = [fromDate timeIntervalSinceReferenceDate];
NSTimeInterval toTime = [toDate timeIntervalSinceReferenceDate];
NSTimeInterval currTime = [[NSDate date] timeIntervalSinceReferenceDate];

NSTimeInterval 基本上是双精度类型,因此您可以比较如果 currTime 大于一个且小于另一个,则日期落在这两个 NSDate 之间。

Do the following,

NSTimeInterval fromTime = [fromDate timeIntervalSinceReferenceDate];
NSTimeInterval toTime = [toDate timeIntervalSinceReferenceDate];
NSTimeInterval currTime = [[NSDate date] timeIntervalSinceReferenceDate];

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文