首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >比较两个nsdatecomponent

比较两个nsdatecomponent
EN

Stack Overflow用户
提问于 2010-10-27 00:09:29
回答 2查看 4.3K关注 0票数 4

如果我有两个nsdatecomponent,我想知道第一个对象的时间是否大于第二个对象的时间。

示例:

代码语言:javascript
复制
if (nsdatecomponentObject1 > nsdatecomponentObject2)
{
    //something
}

因为这种方式不起作用,我怎么能做到呢?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-10-27 00:12:42

NSDateComponentNSDate上使用compare:

代码语言:javascript
复制
if([[nsdatecomponentObject1 date] compare:[nsdatecomponentObject2 date]] == NSOrderedAscending)
票数 7
EN

Stack Overflow用户

发布于 2012-02-16 01:16:48

比较“动态”两个NSDateComponents的正确(也是唯一的)方法是使用NSCalendar对象:

代码语言:javascript
复制
NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];

switch([[gregorian dateFromComponents:oneNSDateComponents] compare:[gregorian dateFromComponents:anotherNSDateComponents]]) {
    case NSOrderedSame:
        NSLog(@"orderSame");
        break;
    case  NSOrderedAscending:
        NSLog(@"orderAscending");
        break;
    case  NSOrderedDescending:
        NSLog(@"orderDescending");
        break;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4025584

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档