iOS比较两个日期的大小
iOS比较两个日期的大小
//比较两个日期的大小 日期格式为2016-08-14 08:46:20
NSLog(@"^^^^^^^^%ld",[self compareWithDate:@"2019-08-14 08:46:20"]);
- (NSInteger)compareWithDate:(NSString*)bDate{
//2017-04-24 08:57:29 得到当前时间date
NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
[formattersetDateFormat:@"yyyy-MM-dd hh:mm:ss"];
NSString*aDate=[formatterstringFromDate:[NSDatedate]];
NSDateFormatter *dateformater = [[NSDateFormatter alloc] init];
[dateformatersetDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate*dta = [[NSDatealloc]init];
NSDate*dtb = [[NSDatealloc]init];
dta = [dateformaterdateFromString:aDate];
dtb = [dateformaterdateFromString:bDate];
NSComparisonResultresult = [dtacompare:dtb];
if (result == NSOrderedDescending) {
//指定时间 已过期
return1;
}
elseif(result ==NSOrderedAscending){
//指定时间 没过期
return-1;
}else{
//刚好时间一样.
return0;
}
}
NSLog(@"****%ld",[self compareDate:@"2018-08-14 08:46:20" withDate:@"2018-09-14 08:46:20"]);
//比较两个日期的大小 日期格式为2016-08-14 08:46:20
- (NSInteger)compareDate:(NSString*)aDate withDate:(NSString*)bDate{
NSDateFormatter *dateformater = [[NSDateFormatter alloc] init];
[dateformatersetDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate*dta = [[NSDatealloc]init];
NSDate*dtb = [[NSDatealloc]init];
dta = [dateformaterdateFromString:aDate];
dtb = [dateformaterdateFromString:bDate];
NSComparisonResultresult = [dtacompare:dtb];
if (result == NSOrderedDescending) {
//指定时间 已过期
return1;
}
elseif(result ==NSOrderedAscending){
//指定时间 没过期
return-1;
}else{
//刚好时间一样.
return0;
}
}