How to check if date (from date picker) is between two other dates ? objective-c

后端 未结 3 635
醉话见心
醉话见心 2021-01-27 20:49

i\'m trying to get date from UIDatePicker (MM-dd format) and to check if its in the range of two other dates;

i have tried it in so many different ways and i think my mi

3条回答
  •  情话喂你
    2021-01-27 21:22

    If you have dateA, dateB and dateC, and you want to check if dateA < dateB < dateC, you can get the difference between dateA-dateB and dateB-dateC which will give you diffX and diffY. If diffX sign is equal than diffY sign, it will mean that dateB is between A and C.

    Or, in other words, your code would look like this:

    NSTimeInterval diffX = [dateB timeIntervalSinceDate:dateA];
    NSTimeInterval diffY = [dateC timeIntervalSinceDate:dateB];
    
    if (signbit(diffX) == signbit(diffY))
    {
        // dateB is in-between dateA and dateC
    }
    

提交回复
热议问题