我有两个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检查了两个值时,我的代码总是运行其他
答案 0 :(得分:1)
不要比较代表日期的两个NSString
,而是compare两个NSDate
代替
答案 1 :(得分:0)
如果日期不相等或不相同的格式,条件[test isEqualToString:str]
总是给出NO。所以不要用这个。
使用NSDateComponent
比较两个日期或比较为@Andrey Gordeev
说。
答案 2 :(得分: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);
答案 3 :(得分: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];
}
答案 4 :(得分: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");
}
答案 5 :(得分:0)
尝试使用此代码进行日期之间的任何比较...您不应该以字符串的形式比较日期。将转换前的日期与字符串进行比较。使用格式化程序的dateFromString
函数将字符串“test”转换为日期格式,方法是将确切的日期格式指定为测试的格式。然后使用以下函数比较日期。
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];
}
答案 6 :(得分:-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);
}