0

我有两个 NSString 有我想要的日期,如果它们都有相同的数据,那么如果应该工作,否则应该工作

这是我的代码

     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];

    }

当我使用 NSLog 检查的两个值相同时,我的代码总是运行其他

4

7 回答 7

1

不要比较NSString代表日期的两个 s,而是比较两个NSDates

于 2013-06-28T07:48:31.037 回答
0

[test isEqualToString:str] 如果日期不相等或格式不同,条件总是给出 NO。所以不要用这个。用于NSDateComponent比较两个日期或比较 @Andrey Gordeev 说的。

于 2013-06-28T07:50:13.617 回答
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);
于 2013-06-28T07:52:44.167 回答
0

首先,您将字符串转换为日期并使用它[date1 isEqualToDate:date2]。这是两个日期的比较。

使用此代码我希望此代码对您有用。

 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];

    }
于 2013-06-28T07:54:02.297 回答
0

尝试使用此代码进行日期之间的任何比较...您不应该以字符串的形式比较日期。比较转换为字符串之前的日期。使用格式化程序的功能将字符串“test”转换为日期格式,方法dateFromString是将确切的日期格式指定为测试的日期格式。然后使用以下函数比较日期。

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];

    }
于 2013-06-28T08:32:29.070 回答
0

而不是比较字符串比较日期。像这样,

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");

}
于 2013-06-28T07:55:27.087 回答
-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);


    }
于 2013-06-28T07:49:40.527 回答