【问题标题】:NSDate comparison showing wrong value?NSDate比较显示错误的值?
【发布时间】:2013-06-05 06:48:07
【问题描述】:

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

这是我使用的代码?

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? -___- 和 SS 而不是 ss 秒? -______________________-
  • 好的,我会检查一下

标签: ios objective-c comparison nsdate


【解决方案1】:

用以下代码替换您的代码。 “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");

    }

【讨论】:

    【解决方案2】:

    问题出在日期格式化程序上。使用mm 代替MM 分钟,使用ss 代替SS 秒。

    [dateFormatter1 setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    

    【讨论】:

    • mm 而不是 MM 分钟 -1
    • @borrrden 抱歉打错了。谢谢
    猜你喜欢
    • 2023-04-03
    • 1970-01-01
    • 2013-12-16
    • 1970-01-01
    • 2015-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多