ios - NSDate 比较显示错误的值?

标签 ios objective-c comparison nsdate

我正在比较两个日期,但是当我将这两个日期输入一个字符串变量时它起作用了,但是当我将这些值传递给数据比较方法时它不起作用,为什么?

这是我使用的代码吗?

NSString *dateString =@"2013-05-12 22:10:02";
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:@"yyyy-MM-dd HH:MM:SS"];
NSDate *dateFromString = [[NSDate alloc] init];
// voila!
dateFromString = [dateFormatter dateFromString:dateString];



NSString *dateString1 = @"2013-03-04 07:59:08";
NSDateFormatter *dateFormatter1 = [[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
[dateFormatter1 setDateFormat:@"yyyy-MM-dd HH:MM:SS"];
NSDate *dateFromString1 = [[NSDate alloc] init];
// voila!
dateFromString1 = [dateFormatter dateFromString:dateString1];

if ([dateFromString compare:dateFromString1] == NSOrderedDescending)
{
    NSLog(@"1 is later than 2");

} else if ([dateFromString compare:dateFromString1] == NSOrderedAscending)
{
    NSLog(@"1 is earlier than 2");

} else
{
    NSLog(@"dates are the same");

}

**当我将日期(nsstring 格式)传递给方法时

(int)compareDate:(NSString *)date1:(NSString *)date2 

在那个方法中我写了上面的比较代码 所以我像这样调用这个方法

[self compareDate:date1:date2]

我总是得到“日期相同”的值????当我手动输入日期时,我得到了准确的答案??这是为什么?**

最佳答案

用以下代码替换您的代码。 “mm”格式用于分钟,“MM”用于月。并使用“ss”表示秒而不是“SS”。阅读苹果文档以获取有关日期格式化程序的详细信息。

 NSString *dateString =@"2013-05-12 22:10:02";
    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:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *dateFromString = [[NSDate alloc] init];
    // voila!
    dateFromString = [dateFormatter dateFromString:dateString];



    NSString *dateString1 = @"2013-03-04 07:59:08";
    NSDateFormatter *dateFormatter1 = [[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
    [dateFormatter1 setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *dateFromString1 = [[NSDate alloc] init];
    // voila!
    dateFromString1 = [dateFormatter dateFromString:dateString1];

    if ([dateFromString compare:dateFromString1] == NSOrderedDescending)
    {
        NSLog(@"1 is later than 2");

    } else if ([dateFromString compare:dateFromString1] == NSOrderedAscending)
    {
        NSLog(@"1 is earlier than 2");

    } else
    {
        NSLog(@"dates are the same");

    }

关于ios - NSDate 比较显示错误的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16933297/

相关文章:

iphone - SOAP 响应不完整

ios - swift 中的多个枚举值

javascript - uiwebview 缩放文本

c - C中的直接调用与间接调用

python - 比较在 python 中作为输入传递的 2 个 sha512 哈希值

iOS APIS - 'Reference'

ios - 使用 Xcode 在 iOS 中注册 Login Logout App(NSUserDefaults)

带有查询的 iOS Segue 动画初学者

C 中的字符比较

ios - 如何使用调试配置指定项目的自定义 xcconfig