我从数据库表中获取日期。我从数据库获取duedate,直到这里一切正常 现在我必须将此duedate与当前日期进行比较,如果当前日期大于或等于来自数据库的duedate然后我将在单元格上打印字符串为'Due'
我尝试很多,但我的问题是显示一些商品的字符串值,他们没有显示一些商品 什么是我的代码中的错误包。
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"dd/MM/yyyy"];
NSString* duedate = [dateFormat stringFromDate:taskobject.DueDate];
NSDate *currentdate;
currentdate=[NSDate date];
//NSDateFormatter *dateFormat1 = [[NSDateFormatter alloc] init];
//[dateFormat1 setDateFormat:@"dd/MM/yyyy"];
NSString* duedate1 = [dateFormat stringFromDate:currentdate];
if ([currentdate compare:taskobject.DueDate]!=NSOrderedDescending) {
DateLable.text=@"Due";
DateLable.textColor=[UIColor redColor];
}
else{
DateLable.text=duedate;
DateLable.textColor=[UIColor blackColor];
}
答案 0 :(得分:2)
NSComparisonResult result = [currentdate compare: duedate];
if( result == NSOrderedDescending || result==NSOrderedSame) {
// true when the dates are the same or the currentdate is later
} else {
// true when the currentdate is earlier
}
答案 1 :(得分:0)
这比较两个日期,并告诉两个日期是否有24小时的差异, timeIntervalSinceDate:lastLoadDate发送秒差。
double interval = [[NSDate date] timeIntervalSinceDate:lastLoadDate];
if (interval > 86400 || interval < -86400 ) {
return YES;
}
else
{
enableButtons = YES;
return NO;
}
在你的情况下它将是
double interval = [dueDate timeIntervalSinceDate:currentDate];
if (interva >=0)
{
DateLable.text=@"Due";
DateLable.textColor=[UIColor redColor];
}
else
{
DateLable.text=duedate;
DateLable.textColor=[UIColor blackColor];
}