【问题标题】:Compare two time values in ios? [duplicate]比较ios中的两个时间值? [复制]
【发布时间】:2014-01-03 11:22:23
【问题描述】:

在我的应用程序中,我想检查当前时间是在保存在变量中的时间之前还是之后。

就像我的 time1 是 time1=@"08:15:12"; 而我的 time2 是 time2=@"18:12:8";

所以我想比较 time1 和 time2。目前这些变量是 NSString 格式。

以下是用来获取时间的代码,我不知道如何比较它们。

代码:

        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        dateFormatter.dateFormat = @"HH:MM:SS";
        [dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
        NSLog(@"ewetwet%@",[dateFormatter stringFromDate:now]);

请帮帮我

【问题讨论】:

标签: ios time nsstring nsdate


【解决方案1】:

你可以使用这个link

根据 Apple 的 NSDate 文档比较:

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

- (NSComparisonResult)compare:(NSDate *)anotherDate

Parameters anotherDate

The date with which to compare the receiver. This value must not be nil. If the value is nil, the behavior is undefined and may change in future versions of Mac OS X.

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

换句话说:

if ([date1 compare:date2]==NSOrderedSame) ...

请注意,这可能更容易阅读和编写:

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

请参阅Apple Documentation 关于这个。如果您觉得有任何问题,也可以使用此链接here。在这里,您可以浏览 Nelson Brian 的答案。

我最后做了如下-

NSDate  * currentDateObj=@"firstDate";
NSDate  * shiftDateFieldObj=@"SecondDate";


NSTimeInterval timeDifferenceBetweenDates = [shiftDateFieldObj timeIntervalSinceDate:currentDateObj];

您还可以根据日期之间的时间差来获取时间间隔。 希望这会有所帮助。

【讨论】:

  • 但是如何将时间值转换为 NSdate?
  • 简单的使用NSDate Formattor 亿欧可以根据你的改变日期
  • 我没有任何日期时间,这可能吗?
  • 是的,首先将其更改为即将到来的格式,然后再次将日期格式化程序设置为您想要的时间格式
【解决方案2】:

首先,您需要将 time 字符串转换为 NSDate => Converting time string to Date format iOS

然后使用以下代码

NSDate *timer1 =...
NSDate *timer2 =...

       NSComparisonResult result = [timer1 compare:timer2];
       if(result == NSOrderedDescending)
       {
          // time 1 is greater then time 2 
       }
       else if(result == NSOrderedAscending)
       {
           // time 2 is greater then time 1 
       }
       else
       {
          //time 1 is equal to time 2
       }

【讨论】:

    【解决方案3】:

    使用以下代码将 nsstring 转换为 nsdate 并进行比较。

    NSString *time1 = @"08:15:12";
    NSString *time2 = @"18:12:08";
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"HH:mm:ss"];
    
    NSDate *date1= [formatter dateFromString:time1];
    NSDate *date2 = [formatter dateFromString:time2];
    
    NSComparisonResult result = [date1 compare:date2];
    if(result == NSOrderedDescending)
    {
        NSLog(@"date1 is later than date2"); 
    }
    else if(result == NSOrderedAscending)
    {
        NSLog(@"date2 is later than date1"); 
    }
    else
    {
        NSLog(@"date1 is equal to date2");
    }
    

    【讨论】:

    【解决方案4】:
    // Use compare method.
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
        NSDate *startDate = [formatter dateFromString:@"2012-12-07 7:17:58"];
        NSDate *endDate = [formatter dateFromString:@"2012-12-07 7:17:59"];
    
    
        if ([startDate compare: endDate] == NSOrderedDescending) {
            NSLog(@"startDate is later than endDate");
    
        } else if ([startDate compare:endDate] == NSOrderedAscending) {
            NSLog(@"startDate is earlier than endDate");
    
        } else {
            NSLog(@"dates are the same");
    
        }
    
        // Way 2
        NSTimeInterval timeDifference = [endDate timeIntervalSinceDate:startDate];
    
        double minutes = timeDifference / 60;
        double hours = minutes / 60;
        double seconds = timeDifference;
        double days = minutes / 1440;
    
        NSLog(@" days = %.0f,hours = %.2f, minutes = %.0f,seconds = %.0f", days, hours, minutes, seconds);
    
        if (seconds >= 1)
            NSLog(@"End Date is grater");
        else
            NSLog(@"Start Date is grater");
    

    【讨论】:

      猜你喜欢
      • 2020-06-18
      • 2015-12-05
      • 1970-01-01
      • 2018-01-10
      • 2013-03-20
      • 1970-01-01
      • 2016-08-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多