Difference between two NSDate objects — Result also a NSDate

后端 未结 5 1027
礼貌的吻别
礼貌的吻别 2020-11-30 20:38

I have two NSDate objects and I want the difference between the two and the result should again be a NSDate object. Any idea how to achieve this?

Here, I am trying t

5条回答
  •  一个人的身影
    2020-11-30 20:55

    There is a easy way by using -compare: in NSDate:

    NSDate *dateA = [NSDate dateWithTimeIntervalSinceNow:100];
    NSDate *dateB = [NSDate dateWithTimeIntervalSinceNow:200];
    NSDate *myDate = [NSDate dateWithTimeIntervalSinceNow:150];
    NSArray *dateArray = [NSArray arrayWithObjects:dateA, dateB, myDate, nil];
    NSArray *sortedArray = [dateArray sortedArrayUsingSelector:@selector(compare:)];
    if ([myDate isEqualToDate:[sortedArray objectAtIndex:1]])
        NSLog(@"myDatea between dateA and dateB");
    

提交回复
热议问题