If statement with dates

前端 未结 5 812
予麋鹿
予麋鹿 2021-01-16 17:26

what I am trying to do is make a if statement with dates using greater than less than signs. For some reason only the greater than sign works. Here is my code:



        
5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-16 17:52

    NSString objects are objects, and when you compare objects with C comparison operators (==, >, <, etc.) you are comparing their addresses, not their values. You need to use compare, such as:

    if ([dateString compare:@"0810"] == NSOrderedAscending &&
        [dateString compare:@"0800"] == NSOrderedDescending) { ...
    

    Though I'd recommend converting to NSDate objects in most cases if you want to compare dates and times.

提交回复
热议问题