How to compare "today" (NSDate) with formatted string date (dd / MM / yyyy)?

I am new to iOS programming, and I have searched a lot for a way to compare today (NSDate) with formatted string date (dd / MM / yyyy).

I found some good answers with NSDate earlierDate, laterDateetc., but the code does not seem to work in my project.

there he is:

// [book quandd]  is the string :    25/02/2011  (or whatever dd/MM/yyyy date)

NSDate *today = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"dd/MM/yyyy"];

if ( [[dateFormat dateFromString:[book quandd]] isEqualToDate:today]) {
    NSLog(@"Dates are equal");
}
if ( [[dateFormat dateFromString:[book quandd]] earlierDate:today]) {
    NSLog(@"earlier");
}
if ( [[dateFormat dateFromString:[book quandd]] laterDate:today]) {
    NSLog(@"later");
}

No matter what I do and whatever the date in [book quandd], the console always writes β€œearlier” and β€œlater”, as if they were later and earlier at the same time as today.

Where does this error appear?

Is there a simple / simpler way to compare today's date with 02/26/2011, for example?

+3
source share
5 answers

timeIntervalSinceNow:, diff .

, .

0

NSDate , . NSDate , ( ). [NSDate date], "", NSDateFormatter ( ), .

, NSDate , NSCalendar NSDateComponents. "" .

- (void) CompareDate:(NSDate*)date1 toDate:(NSDate*)date2 {
    NSDateFormatter* fmt = [NSDateFormatter new];
    [fmt setDateFormat:@"yyyyMMdd"];
    // Note that you should set the appropriate timezone if you don't want the default.
    NSString* date1Str = [fmt stringFromDate:date1];
    NSString* date2Str = [fmt stringFromDate:date2];
    switch ([date1Str compare:date2Str]) {
        case NSOrderedAscending:
            NSLog(@"Date 1 is earlier");
            break;
        case NSOrderedSame:
            NSLog(@"Dates are equal");
            break;
        case NSOrderedDescending:
            NSLog(@"Date 2 is earlier");
            break;
    }
}
+1

NSDate *today = [NSDate date];
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"dd/MM/yyyy"];

    if ( [[dateFormat dateFromString:[book quandd]] compare:today]==NSOrderedSame) {
        NSLog(@"Dates are equal");
    }
    if ( [[dateFormat dateFromString:[book quandd]] compare:today]==NSOrderedAscending) {
        NSLog(@"earlier");
    }
    if ( [[dateFormat dateFromString:[book quandd]] laterDate:today]==NSOrderedDescending) {
        NSLog(@"later");
    }
0

previousDate . : if ([date1 earlyDate: date2] == date1)

0

Your "mistake" is because laterDate and earlyDate return a date object that is either earlier or later. The returned object is always nonnil, so both if statements are set to true. The correct way to use them is as follows:

NSString *book = @"19/08/2014";

NSDate *today = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"dd/MM/yyyy"];

NSDate *date = [dateFormat dateFromString:book];
if ( [date timeIntervalSinceDate:today] == 0) {
    NSLog(@"Dates are equal");
}
if ( [date laterDate:today] == today ) {
    NSLog(@"earlier");
}
if ( [date laterDate:today] == date) {
    NSLog(@"later");
}

As an alternative, I used timeIntervalSinceDate, which returns seconds between the first date and the second date.

NSString *book = @"19/08/2014";

NSDate *today = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"dd/MM/yyyy"];

NSDate *date = [dateFormat dateFromString:book];
if ( [date timeIntervalSinceDate:today] == 0) {
    NSLog(@"Dates are equal");
}
if ( [date timeIntervalSinceDate:today] < 0 ) {
    NSLog(@"earlier");
}
if ( [date timeIntervalSinceDate:today] > 0) {
    NSLog(@"later");
}
0
source

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


All Articles