【问题标题】:How can i compare two dates? [duplicate]如何比较两个日期? [复制]
【发布时间】:2013-05-30 16:40:04
【问题描述】:

我正在尝试比较两个日期,无论哪个是新日期还是旧日期,我的日期格式如下:

2013-06-03 09:30:35

(YYYY-MM-DD HH:MM:SS)

请给我一些想法,目前都是字符串格式?

那么可以转换广告支票吗?

【问题讨论】:

    标签: ios objective-c nsdate


    【解决方案1】:

    使用dateFormatter 将该字符串转换为NSDate。 您可以查看此链接 这是对您的问题的良好讨论 How to compare two NSDates: Which is more recent?

    【讨论】:

    • 如果您认为某个问题重复,请将其标记为重复,而不是使用指向其他问题和答案的链接进行回答。
    • 好的,但我没有看到任何标记重复的选项。
    • 在关闭链接下方。它为您提供了为什么建议关闭的选项。原因之一是它是重复的。您可以将链接粘贴到其中的副本。
    • 感谢您的回答
    • @user2439088:尊重他人.... Abizern 帮助你
    【解决方案2】:

    试试这个

    NSString 到 NSDate

    NSString *dateString = @"01-02-2010";
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    // this is imporant - we set our input date format to match our input string
    // if format doesn't match you'll get nil from your string, so be careful
    [dateFormatter setDateFormat:@"dd-MM-yyyy"];
    NSDate *dateFromString = [[NSDate alloc] init];
    // voila!
    dateFromString = [dateFormatter dateFromString:dateString];
    [dateFormatter release];
    

    NSDate 转换为 NSString:

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"dd-MM-yyyy"];
    NSString *strDate = [dateFormatter stringFromDate:[NSDate date]];
    NSLog(@"%@", strDate);
    [dateFormatter release];
    

    请阅读本文

    enter link description here

    if ([yourFirstDate compare:secondDate] == NSOrderedDescending) {
        NSLog(@"yourFirstDate is later than secondDate");        
    
    } else if ([yourFirstDate compare:secondDate] == NSOrderedAscending) {
        NSLog(@"yourFirstDate is earlier than secondDate");
    
    } else {
        NSLog(@"dates are the same");
    
    }
    

    与@DIVYU 的链接相同

    【讨论】:

    • 你遗漏了关于比较的部分。
    • 谢谢,但我该如何比较?
    • 好吧,干得好。让我试试这个
    • 如果与重复链接相同,则将问题标记为重复。这是这个网站应该工作的方式。重复的问题只会让正确的问题更难得到正确的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-13
    • 2011-08-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多