如何比较两个日期?

发布于 2024-08-20 05:33:22 字数 107 浏览 5 评论 0原文

我有三个日期:(1) previousDate (2) currentDate (3) nextDate, 我想检查 currentDate 是否晚于前一个日期且早于 nextDate。 我该怎么做?

I have three dates: (1) previousDate (2) currentDate (3) nextDate,
I want to check whether currentDate is later then previous date and earlier than nextDate.
How do I do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

禾厶谷欠 2024-08-27 05:33:22
 NSDateFormatter *df= [[NSDateFormatter alloc] init];

[df setDateFormat:@"yyyy-MM-dd"];

NSDate *dt1 = [[NSDate alloc] init];

NSDate *dt2 = [[NSDate alloc] init];

dt1=[df dateFromString:@"2011-02-25"];

dt2=[df dateFromString:@"2011-03-25"];

NSComparisonResult result = [dt1 compare:dt2];

switch (result)

{

     case NSOrderedAscending: NSLog(@"%@ is greater than %@", dt2, dt1); break;

     case NSOrderedDescending: NSLog(@"%@ is less %@", dt2, dt1); break;

     case NSOrderedSame: NSLog(@"%@ is equal to %@", dt2, dt1); break;

     default: NSLog(@"erorr dates %@, %@", dt2, dt1); break;
}
 NSDateFormatter *df= [[NSDateFormatter alloc] init];

[df setDateFormat:@"yyyy-MM-dd"];

NSDate *dt1 = [[NSDate alloc] init];

NSDate *dt2 = [[NSDate alloc] init];

dt1=[df dateFromString:@"2011-02-25"];

dt2=[df dateFromString:@"2011-03-25"];

NSComparisonResult result = [dt1 compare:dt2];

switch (result)

{

     case NSOrderedAscending: NSLog(@"%@ is greater than %@", dt2, dt1); break;

     case NSOrderedDescending: NSLog(@"%@ is less %@", dt2, dt1); break;

     case NSOrderedSame: NSLog(@"%@ is equal to %@", dt2, dt1); break;

     default: NSLog(@"erorr dates %@, %@", dt2, dt1); break;
}
听风吹 2024-08-27 05:33:22

我想您正在使用 NSDate 类。

您可以使用 isEqualToDate 比较两个 NSDate 对象。还有 earlierDatelaterDate 检查当前日期是否大于上一个日期且小于下一个日期。

I suppose you're using the NSDate class.

You can use isEqualToDate to compare two NSDate objects. And also the earlierDate and laterDate to check currentdate is bigger than previous date and smaller than next date.

你爱我像她 2024-08-27 05:33:22

我只是用它来检查一下:

if ([[currentDate laterDate:nextDate] isEqualToDate:nextDate]) {
    NSLog(@"currentDate is earlier than nextDate");
}
if ([[currentDate laterDate:previousDate] isEqualToDate:currentDate]) {
    NSLog(@"currentDate is later then previousDate");
}

对我来说效果很好!谢谢@Luca Matteis 的提示“稍后日期:”

I simply used this to check it:

if ([[currentDate laterDate:nextDate] isEqualToDate:nextDate]) {
    NSLog(@"currentDate is earlier than nextDate");
}
if ([[currentDate laterDate:previousDate] isEqualToDate:currentDate]) {
    NSLog(@"currentDate is later then previousDate");
}

worked fine for me! Thx @Luca Matteis for the hint "laterDate:"

梦里南柯 2024-08-27 05:33:22

我们可以在 swift 5.0 中轻松比较两个日期

    switch dateOld.compare(dateNew) {
    case .orderedAscending:
        // Put your code for greater
        break

    case .orderedDescending:
        // Put your code less
        break;

    case .orderedSame:
        // Put your code for equal
        break
}

We can compare two dates easily in swift 5.0

    switch dateOld.compare(dateNew) {
    case .orderedAscending:
        // Put your code for greater
        break

    case .orderedDescending:
        // Put your code less
        break;

    case .orderedSame:
        // Put your code for equal
        break
}
梦回旧景 2024-08-27 05:33:22

NSDate 对象还实现了有据可查的 -compare: 方法。

NSDate objects also implement well-documented -compare: method.

把梦留给海 2024-08-27 05:33:22

只需使用 NSDatecompare 消息执行两次比较:

if ([previousDate compare:currentDate] == NSOrderedAscending &&
    [nextDate compare:currentDate] == NSOrderedDescending) {
    NSLog(@"current date is in between previous and next date (non-inclusive)");
}

Just perform two comparisons using the compare message of NSDate:

if ([previousDate compare:currentDate] == NSOrderedAscending &&
    [nextDate compare:currentDate] == NSOrderedDescending) {
    NSLog(@"current date is in between previous and next date (non-inclusive)");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文