比较两个日期

发布于 2024-11-02 14:04:21 字数 149 浏览 1 评论 0原文

如何比较两个不同的日期以找出较晚的日期?例如,在date1中,我将在下载一些数据后存储一个日期,在date2中,我将存储当前日期。然后我需要检查哪个更大/更晚:类似于 if(date1>date2)

How can I compare two different dates to find out which is the later date? For example, in date1 I will store one date after downloading some data, and in date2, I will store the current date. Then I need to check which one is greater/later: something like if(date1>date2).

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

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

发布评论

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

评论(4

驱逐舰岛风号 2024-11-09 14:04:21

像这样的东西:

    NSDate* timeNow = [NSDate date];

    // If less than 30 seconds, do something
    if ([timeNow timeIntervalSinceDate:anEarlierTime] < 30.0f)
    {
        // Do something
    }

Something like:

    NSDate* timeNow = [NSDate date];

    // If less than 30 seconds, do something
    if ([timeNow timeIntervalSinceDate:anEarlierTime] < 30.0f)
    {
        // Do something
    }
哆兒滾 2024-11-09 14:04:21

您可以使用 NSDate 的比较:方法:

NSComparisonResult result = [Date2 compare:Date1];

if(result==NSOrderedAscending)
    NSLog(@"Date1 is in the future");
else if(result==NSOrderedDescending)
    NSLog(@"Date1 is in the past");
else
    NSLog(@"Both dates are the same");

you can use NSDate's compare: method:

NSComparisonResult result = [Date2 compare:Date1];

if(result==NSOrderedAscending)
    NSLog(@"Date1 is in the future");
else if(result==NSOrderedDescending)
    NSLog(@"Date1 is in the past");
else
    NSLog(@"Both dates are the same");
Hello爱情风 2024-11-09 14:04:21
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-11-09 14:04:21

这是我用来比较日期和时区的方法,并使用 iOS 6 为我工作

        NSComparisonResult result = [timeStamp compare:serverDate];
        NSDateFormatter *df = [[NSDateFormatter alloc] init];
        [df setDateFormat:@"yyyy-MM-dd HH:mm:ss z"];
        switch (result)
        {
            case NSOrderedAscending:
                NSLog(@"%@ is greater than %@", [df stringFromDate:serverDate], [df stringFromDate:timeStamp]);
                break;
            case NSOrderedDescending:
                NSLog(@"%@ is less %@", [df stringFromDate:serverDate], [df stringFromDate:timeStamp]);
                break;
            case NSOrderedSame:
                NSLog(@"%@ is equal to %@", [df stringFromDate:serverDate], [df stringFromDate:timeStamp]);
                break;
            default:
                NSLog(@"erorr dates %@, %@", [df stringFromDate:serverDate], [df stringFromDate:timeStamp]);
                break;
        }

This is what I used for comparing dates with timezones, and worked for me using iOS 6

        NSComparisonResult result = [timeStamp compare:serverDate];
        NSDateFormatter *df = [[NSDateFormatter alloc] init];
        [df setDateFormat:@"yyyy-MM-dd HH:mm:ss z"];
        switch (result)
        {
            case NSOrderedAscending:
                NSLog(@"%@ is greater than %@", [df stringFromDate:serverDate], [df stringFromDate:timeStamp]);
                break;
            case NSOrderedDescending:
                NSLog(@"%@ is less %@", [df stringFromDate:serverDate], [df stringFromDate:timeStamp]);
                break;
            case NSOrderedSame:
                NSLog(@"%@ is equal to %@", [df stringFromDate:serverDate], [df stringFromDate:timeStamp]);
                break;
            default:
                NSLog(@"erorr dates %@, %@", [df stringFromDate:serverDate], [df stringFromDate:timeStamp]);
                break;
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文