【问题标题】:How to compare two times in iOS如何在iOS中比较两次
【发布时间】:2013-05-21 05:32:15
【问题描述】:

我试图在 iOS 中比较两次。我的数组中有时间列表,我只想检查当前时间是否与数组值中的任何一个匹配。可以帮助如何做到这一点。我搜索了整个互联网我没有得到任何想法。

我看到了this question answer,但我无法得到确切的结果。

【问题讨论】:

  • 当前时间的精度是多少?我怀疑[datesArray containsObject:[NSDate date]] 会开箱即用......
  • 检查这可能会给你一些想法。 stackoverflow.com/questions/13229142/…
  • 您尝试过 NSDate 比较方法吗?如果它返回 NSOrderedSame 则日期相同
  • NSDate compare 几乎永远不会返回 NSOrderedSame 因为它比较所有元素,包括秒、微秒等。这个问题的措辞很糟糕,需要更多的细节来适当地回答。
  • 您搜索了whole -- entire -- internet 却没有任何个关于如何比较时间的想法?我根本不相信你。

标签: cocoa-touch date


【解决方案1】:

根据 Apple 的 NSDate 文档比较:

Returns an NSComparisonResult value that indicates the temporal ordering of the receiver and another given date.

- (NSComparisonResult)compare:(NSDate *)anotherDate

参数 anotherDate

与接收者进行比较的日期。此值不能为 nil。

Return Value

If:

The receiver and anotherDate are exactly equal to each other, NSOrderedSame

The receiver is later in time than anotherDate, NSOrderedDescending

The receiver is earlier in time than anotherDate, NSOrderedAscending

In other words:

if ([date1 compare:date2]==NSOrderedSame) ...
Note that it might be easier to read and write this :

if ([date2 isEqualToDate:date2]) ...

【讨论】:

    【解决方案2】:
    -(NSString *)determineDateFromstring:(long long)date
    {
        NSTimeInterval interval=date;
        NSDate *currentTime=[NSDate dateWithTimeIntervalSince1970:interval/1000];
    
        NSLocale *gbLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_GB"];
    
        NSNumber *myDateInString=[NSNumber numberWithLongLong:[[NSDate date] timeIntervalSince1970]*1000];
        NSTimeInterval inte=[myDateInString longLongValue];
        NSDate *todayTime=[NSDate dateWithTimeIntervalSince1970:inte/1000];
    
    
    
        NSCalendar *currentcalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
        NSDateComponents *components = [currentcalendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:todayTime];
    
        int currentday=[components day];
        int currentyear=[components year];
    
        NSCalendar *paramtercalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
        NSDateComponents *parametercomponents = [paramtercalendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:currentTime];
    
        int currentday1=[parametercomponents day];
        int currentyear1=[parametercomponents year];
    
    
        if (currentday==currentday1) {
         Nslog(date are same);  
    
        }else if(currentyear==currentyear1) {
         Nslog(year are same);
    
        }else{
    
        }
    
    }
    

    如果你想检查两个日期是否相等,那么你可以比较 NStimeIntervals(long long 值)。如果相同则时间、日期和年份相同,否则不同。

    我希望这个答案对你的问题是正确的......

    【讨论】:

      【解决方案3】:

      你为什么不检查自 1970 年以来的间隔并比较整数值..

          long long int i = [[NSDate date] timeIntervalSince1970];
      

      我希望这会有所帮助。此外,如果您有 NSString 形式的值,那么您首先需要将它们转换为 NSDate

              NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
              dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ssSSSSSS";  
              NSDate *d = [dateFormatter dateFromString:@"your date string"];
      

      【讨论】:

      • 改为直接用timeIntervalSinceDate:检查两个日期之间的时间间隔。此外,NSTimeIntervaldouble,而不是整数。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-24
      相关资源
      最近更新 更多