简体   繁体   English

如何比较目标c中的日期

[英]how to compare dates in objective c

I want compare two dates one would be the current date and other date stored in database. 我想比较两个日期,一个是当前日期,另一个是存储在数据库中的日期。 So I want to find out which date is grater from both. 因此,我想找出两个日期比较合适的日期。

You can use the compare method of the NSDate class 您可以使用NSDate类的compare方法

[myDate compare:someOtherDate]

This will return one of three values of type NSComparisonResult... 这将返回NSComparisonResult类型的三个值之一。

  • NSOrderedSame if both dates are equal NSOrderedSame如果两个日期相等
  • NSOrderedDescending if myDate comes after someOtherDate 如果myDate在someOtherDate之后,则NSOrderedDescending
  • NSOrderedAscending if myDate comes before someOtherDate 如果myDate在someOtherDate之前,则NSOrderedAscending

Check apple documentation . 检查Apple 文档

You can use the following methods, 您可以使用以下方法,

  • – isEqualToDate:
  • – earlierDate:
  • – laterDate:
  • – compare:

You can use compare: method to find which date is earlier or later check the documentation on this 您可以使用compare:方法来查找哪个日期是更早或更晚的日期,请查看此文档

http://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSDate_Class/ http://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSDate_Class/

Edit: 编辑:

NSDateFormatter * formatter= [NSDateFormatter alloc] init];
NSDate *date = [formatter dateFromString:stringDate];
[formatter release];

then compare them. 然后比较它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM