0

I have two NSString which have date i want that if they both have same data then if should work otherwise else should work

here is my code

     NSString*test=data.addedDateTime;



    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy_MM_dd"];

    NSDate* date = [NSDate date];

    NSString* str = [formatter stringFromDate:date];
    NSString*addedDateTime=str;



    if ([test isEqualToString:str]) {


        [todayArray addObject:theObject];

        int count=[todayArray count];

        NSLog(@"Today array working %d",count);


    }

    else {

        [yesterdayArray addObject:theObject];

    }

my code always runs else when there is same value in both i have checked using NSLog

Andrey Gordeev
  • 30,606
  • 13
  • 135
  • 162
Jaw Ali
  • 205
  • 5
  • 13

7 Answers7

1

Don't compare two NSStrings, which represent the dates, but compare two NSDates instead

Community
  • 1
  • 1
Andrey Gordeev
  • 30,606
  • 13
  • 135
  • 162
0

Condition [test isEqualToString:str] always gives NO if date is either not equal or in not same formate. So dont use this. Use NSDateComponent to compare two dates or Compare as @Andrey Gordeev Says .

Dilip Manek
  • 9,095
  • 5
  • 44
  • 56
Satish Azad
  • 2,302
  • 1
  • 16
  • 35
0
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
                [dateFormatter setDateFormat:@"yyyy-MM-dd"];
                NSDate *date1 = [dateFormatter dateFromString:TodatDate];

                NSDateFormatter *dateFormatte = [[[NSDateFormatter alloc] init] autorelease];
                [dateFormatte setDateFormat:@"yyyy-MM-dd"];
                NSDate *date2 = [dateFormatte dateFromString:ServerDate];

                unsigned int unitFlags = NSDayCalendarUnit;

                NSCalendar *gregorian = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
                NSDateComponents *comps = [gregorian components:unitFlags fromDate:date1  toDate:date2  options:0];

                int days = [comps day];
                NSLog(@"%d",days);
Bhavesh Nayi
  • 3,626
  • 1
  • 27
  • 42
0

First you convert string to date and use this [date1 isEqualToDate:date2].This is a compair a two date.

use this code I hope this code useful for you.

 NSString*test=data.addedDateTime;



    NSDateFormatter* df = [[NSDateFormatter alloc] init];
            [df setDateFormat:@"yyyy_MM_dd"];
            NSDate* d = [df dateFromString:test];
            [df release];

    NSDate* date = [NSDate date];


    if ([d isEqualToDate:date) {


        [todayArray addObject:theObject];

        int count=[todayArray count];

        NSLog(@"Today array working %d",count);


    }

    else {

        [yesterdayArray addObject:theObject];

    }
Darshan Kunjadiya
  • 3,323
  • 1
  • 29
  • 31
0

Instead of comparing String compare Date. Like this,

NSDate *myDate1 =data.addedDateTime;
NSDate* mydate2 = [NSDate date];

if ([myDate1 compare:mydate2] == NSOrderedDescending) {
    NSLog(@"Dilip myDate1 is later than myDate2");

} else if ([myDate1 compare:mydate2] == NSOrderedAscending) {
    NSLog(@"Dilip myDate1 is earlier than myDate2");

} else {
    NSLog(@"Dilip dates are the same");

}
Dilip Manek
  • 9,095
  • 5
  • 44
  • 56
  • -[__NSDate length]: unrecognized selector sent to instance 0x5b54ac0 – Jaw Ali Jun 28 '13 at 08:05
  • @JawAli check my answer now i have edited it..Sorry for late reply on lunch break..Now i have tested the code its running perfectly..If still get error than check that `data.addedDateTime` is valid or not. – Dilip Manek Jun 28 '13 at 09:34
0

Try this code for any comparison between dates... You should not compare date in the form of string. Compare the dates before conversion to string. Convert the string "test" into date format using dateFromString function of the formatter by specifying the exact date format as that of the test. Then compare the dates using following function.

NSString*test=data.addedDateTime;
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@"yyyy_MM_dd"];

NSDate *newDate = [formatter dateFromString:data.addedDateTime]; // other date 
NSDate *today = [NSDate date]; // current date

NSComparisonResult result; 

result = [today compare:newDate]; // comparing two dates

if(result == NSOrderedSame)
    {
    [todayArray addObject:theObject];

    int count=[todayArray count];

    NSLog(@"Today array working %d",count);
}
else
    {

    [yesterdayArray addObject:theObject];

    }
prince
  • 854
  • 2
  • 9
  • 36
-1
first u should convert date to nsstring..
then u set perfect date formate in both string..
like
 NSDate *today1 = [NSDate date];
  NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
 [dateFormat setDateFormat:@"dd-MM-yyyy"];
  NSString *dateString11 = [dateFormat stringFromDate:today1];
  [dateString11 retain];
ur first string is like dateString11

and other date string is like str2=@"03-10-2010";

then u compare two string

 if ([dateString11 isEqualToString:str2]) {


        [todayArray addObject:theObject];

        int count=[todayArray count];

        NSLog(@"Today array working %d",count);


    }
2014
  • 119
  • 1
  • 1
  • 13