#pragma mark -得到当前时间
- (NSDate *)CurrentTime{
NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
//当前时间格式
[formatter setDateFormat:@"yyyy-MM-dd"];
NSString *dateTime=[formatter stringFromDate:[NSDate date]];
NSDate *date = [formatter dateFromString:dateTime];
NSLog(@"---------- currentDate == %@",date);
return date;
}
#当前时间和后台返回的时间比较方法,返回一个int类型的数据
//oneDay:当前时间
//anotherDay:返回时间
- (int)compareCurrentTimeDay:(NSDate *)oneDay withAnotherDay:(NSDate *)anotherDay
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//这个时间格式需要和当前得到时间格式相同才能做比较
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSString *oneDayStr = [dateFormatter stringFromDate:oneDay];
NSString *anotherDayStr = [dateFormatter stringFromDate:anotherDay];
NSDate *dateA = [dateFormatter dateFromString:oneDayStr];
NSDate *dateB = [dateFormatter dateFromString:anotherDayStr];
NSComparisonResult result = [dateA compare:dateB];
NSLog(@"date1 : %@, date2 : %@", oneDay, anotherDay);
if (result == NSOrderedDescending) {
//NSLog(@"Date1 is in the future");
//当前时间大于返回时间
return 1;
}
else if (result == NSOrderedAscending){
//NSLog(@"Date1 is in the past");
//当前时间小于返回时间
return -1;
}
//NSLog(@"Both dates are the same");
//完全相等(NSOrderedDescending,NSOrderedAscending,NSOrderedSame这三个是一个枚举类型)
return 0;
}
iOS当前时间和后台返回时间做比较大小
最新推荐文章于 2023-02-13 14:54:40 发布