我在数据库中有一个字符串日期,我想将其与当前日期进行比较。例如6/13/2013 11:15:00 PM
是数据库中的日期,现在我想将它与今天的日期进行比较。我怎样才能做到这一点?
谢谢..
问问题
185 次
2 回答
1
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"MM/dd/YYYY hh:mm:ss a"];
NSDate *dbDate = [formatter dateFromString:@"your_date_string_from_db"];
if ([dbDate compare:[NSDate date]] == NSComparisonResult_value)
{
//Do what you want accordingly
}
NSComparisonResult_value 是其中之一
enum {
NSOrderedAscending = -1,
NSOrderedSame,
NSOrderedDescending
};
于 2013-06-14T07:43:21.610 回答
1
尝试使用这个
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"MM/dd/yyyy"];
NSDate *dt1 = [[NSDate alloc] init];
NSDate *dt2 = [[NSDate alloc] init];
dt1 = [df dateFromString:yourDatabaseDate];
dt2 = [df dateFromString:[NSDate date]];
NSComparisonResult result = [dt1 compare:dt2];
switch (result)
{
case NSOrderedAscending:
NSLog(@"%@ is greater than %@", dt2, dt1);
break;
case NSOrderedDescending:
NSLog(@"%@ is less %@", dt2, dt1);
break;
case NSOrderedSame:
NSLog(@"%@ is equal to %@", dt2, dt1);
break;
default:
NSLog(@"erorr dates %@, %@", dt2, dt1);
break;
}
于 2013-06-14T07:44:17.730 回答