Comparing Two NSDates Dates and Times Ignoring Seconds

I want to compare two dates and times, ignoring seconds. Below is the code I tried, but didn't seem to succeed. Can someone help me where I'm wrong or provide me with a better solution? Thanks

-(BOOL)checkIfTimePassed{

    NSDate *today = [NSDate date];

    BOOL isTimePassed = [[NSCalendar currentCalendar] compareDate:today toDate:self.whenDate toUnitGranularity:NSCalendarUnitMinute] != NSOrderedAscending;

    return isTimePassed;
}
+4
source share
3 answers

This solution is currently working. If anyone recovers, send him.

-(BOOL)checkIfTimePassed{

    NSDate *today = [NSDate date];

    NSComparisonResult result = [[NSCalendar currentCalendar] compareDate:today toDate:self.whenDate toUnitGranularity:NSCalendarUnitMinute];

    if(result==NSOrderedAscending){
        NSLog(@"today is less");
        return NO;
    }
    else if(result==NSOrderedDescending)
    {
        NSLog(@"newDate is less");
        return YES;
    }
    else
    {
        NSLog(@"Both dates are same");
        return NO;
    }
}
0
source

Your comparison is disabled. Try this instead. Pay attention to the change of parameters and comparison.

-(BOOL)checkIfTimePassed{

    NSDate *today = [NSDate date];

    BOOL isTimePassed = [[NSCalendar currentCalendar] compareDate:self.whenDate toDate:today toUnitGranularity:NSCalendarUnitMinute] == NSOrderedAscending;

    return isTimePassed;
}
+4
source
NSDate *startDate = [NSDate date];
NSDate *endDate = [startDate dateByAddingTimeInterval:+7*24*60*60];

NSDateFormatter *df=[[NSDateFormatter alloc] init];


    [df setDateFormat:@"yyyy-MM-dd hh:mm a"];



- (BOOL)validateDates:(NSDate*)startDate EndDate:(NSDate*)endDate{
    NSComparisonResult dateCompareResutFinal = [startDate compare:endDate];

    if (dateCompareResutFinal == NSOrderedDescending) {
        [RKDropdownAlert dismissAllAlert];
        [RKDropdownAlert title:AlertNotificationError message:@"Start date should be less than End date" backgroundColor:[UIColor colorWithRed:0.0/255.0 green:0.0/255.0 blue:0.0/255.0 alpha:0.7] textColor:[UIColor whiteColor] time:2 delegate:self]
        ;
        return NO;
    }else if (dateCompareResutFinal == NSOrderedSame) {
        [RKDropdownAlert dismissAllAlert];
        [RKDropdownAlert title:AlertNotificationError message:@"Both dates cannot be same" backgroundColor:[UIColor colorWithRed:0.0/255.0 green:0.0/255.0 blue:0.0/255.0 alpha:0.7] textColor:[UIColor whiteColor] time:2 delegate:self];

        return NO;
    }else{
        return YES;
    }
}
0
source

Source: https://habr.com/ru/post/1693764/


All Articles