比较两个日期时出错

时间:2014-07-12 19:48:16

标签: objective-c

 NSDate *today = [NSDate date];
 NSDate *staticDate = [NSDate dateWithString:@"2014-07-12"];

 NSComparisonResult result;

 result = [today compare:staticDate];

 if(result == NSOrderedSame)
    {
        self.t1.text = @"hello";
    }

我试图将日期与当前日期进行比较,但我在dateWithString方法中遇到错误,它说"没有已知的dateWithString类方法:"

1 个答案:

答案 0 :(得分:0)

错误是因为NSDate没有方法dateWithString。我认为你可以更好地使用NSDateFormatter实例来实现这个

来自docs

  

NSDateFormatter的实例创建NSDate的字符串表示   (和NSCalendarDate)对象,并转换文本表示   进入NSDate对象的日期和时间。

示例:

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