比较两个日期
2018-03-06 本文已影响4人
莫失莫忘__不离不弃
- 判断两个时间的先后顺序
-
(BOOL)judgeTheOntTime:(NSString *)firstString isLaterThanAnotherTime:(NSString *)Secondstring
{
NSDateFormatter *dateformatter = [[NSDateFormatter alloc]init];
[dateformatter setDateFormat:@"yyyy-mm"];NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
[dateformatter setTimeZone:timeZone];NSDate *oneTime = [dateformatter dateFromString:firstString];
NSDate *anotherTime = [dateformatter dateFromString:Secondstring];//第一个时间晚于第二个时间
if ([oneTime timeIntervalSinceDate:anotherTime] > 0) {return YES;
}else{
return NO;
}
}
2.调用两个时间进行判断
NSString *firstDate = @"2017-10";
NSString *secDate = @"2017-11";
BOOL isForer = [self judgeTheOntTime:firstDate isLaterThanAnotherTime:secDate];
if (isForer) {
NSLog(@"成功,早于");
}else{
NSLog(@"成功,晚于");
}
3.截图如下
image.png
image.png