If statement with dates

◇◆丶佛笑我妖孽 提交于 2019-12-30 14:20:55

问题


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:

NSDate *currDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"HHmm"];
NSString *dateString = [dateFormatter stringFromDate:currDate];
NSLog(@"%@",dateString);

if (dateString < @"0810" && dateString > @"0800") {
    NSLog(@"Homeroom");
}
else {
    NSLog(@"no");
}

The output for this code would be if the time was 8:03:

2013-04-08 08:03:47.956 Schedule2.0[13200:c07] 0803
2013-04-08 08:03:47.957 Schedule2.0[13200:c07] no

If I were to make is so where it is only the greater then sign like this:

if (dateString > @"0800") {
    NSLog(@"Homeroom"); 
}
else {
    NSLog(@"no");
}

The output would be this:

2013-04-08 08:03:29.748 Schedule2.0[14994:c07] 0803
2013-04-08 08:03:29.749 Schedule2.0[14994:c07] Homeroom

回答1:


create a NSDate object with the time 8:10 and one with 8:00. Now you can compare the given date with both these dates

if(([date0800 compare:date] == NSOrderingAscending) && [date0810 compare:date] == NSOrderingDescending) )
{
    // date is between the other
}

to create the boundaries dates you can do this

NSDate *date = [NSDate date]; // now
NSDateComponents *components = [[NSCalendar currentCalendar] components:( NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit ) fromDate:date];
components.hour = 8;
components.minute = 0;

NSDate *date0800 = [[NSCalendar currentCalendar] dateFromComponents: components];
components.minute = 10;
NSDate *date0810 = [[NSCalendar currentCalendar] dateFromComponents: components];

if you insist of using operators like < and >, you can use the timeinterval of the date objects.

if(([date0800 timeIntervalSince1970] < [date timeIntervalSince1970]) && ([date0810 timeIntervalSince1970] > [date timeIntervalSince1970]))
{
    // date lays between the other two
}

but beware of checking == on it, as it could be faulty due to rounding errors.




回答2:


Here you are comparing string objects, with < and >, which does not do what you are expecting. You can use NSDateComponents to get the hour and minute to compare those:

NSDate *today = [NSDate date];

NSCalendar *gregorian = [[NSCalendar alloc]

                     initWithCalendarIdentifier:NSGregorianCalendar];

NSDateComponents *components =

                [gregorian components:(NSHourCalendarUnit | NSMinuteCalendarUnit ) fromDate:today];

NSInteger hour = [weekdayComponents hour];

NSInteger minutes = [weekdayComponents minute];

BOOL homeroom = (hour == 8) && (minute < 10);

Or you can create a specific NSDate for 8:10 and 8:00 using NSDateFormater and using the compare: function.




回答3:


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.




回答4:


You can't use > or < to compare string objects. That actually compares pointers so we won't get into why > 'works' and < 'doesn't'.

For this kind of date comparison use NSDateComponents NSDateComponents Reference




回答5:


Here's the gist of a category I wrote on NSDate. I found it made my code more readable.

https://gist.github.com/nall/5341477

@interface NSDate(SZRelationalOperators)
-(BOOL)isLessThan:(NSDate*)theDate;
-(BOOL)isLessThanOrEqualTo:(NSDate*)theDate;
-(BOOL)isGreaterThan:(NSDate*)theDate;
-(BOOL)isGreaterThanOrEqualTo:(NSDate*)theDate;
@end


来源:https://stackoverflow.com/questions/15890205/if-statement-with-dates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!