ios 比较两个日期格式(NSString,NSDate)的大小/前后

方法已封装!日期格式请传入:2013-08-05 12:12:12;如果修改日期格式,比如:2013-08-05,则将[df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];修改为[df setDateFormat:@"yyyy-MM-dd"];

-(int)compareDate:(NSString*)date01 withDate:(NSString*)date02{

int ci;

NSDateFormatter *df = [[NSDateFormatter alloc] init];

[df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

NSDate *dt1 = [[NSDate alloc] init];

NSDate *dt2 = [[NSDate alloc] init];

dt1 = [df dateFromString:date01];

dt2 = [df dateFromString:date02];

NSComparisonResult result = [dt1 compare:dt2];

switch (result)

{

//date02比date01大

case NSOrderedAscending: ci=1; break;

//date02比date01小

case NSOrderedDescending: ci=-1; break;

//date02=date01

case NSOrderedSame: ci=0; break;

default: NSLog(@"erorr dates %@, %@", dt2, dt1); break;

}

return ci;

}

你可能感兴趣的:(ios 比较两个日期格式(NSString,NSDate)的大小/前后)