美文网首页
iOS中时间日期的比较

iOS中时间日期的比较

作者: 9bf19a4010ab | 来源:发表于2017-01-14 16:55 被阅读28次

举一个例子相信大家就都懂了
例: 当前时间于某个固定时间进行比较

 // 当前时间
 NSDate *currentDate = [NSDate date]; // 获得时间对象
 NSDateFormatter *forMatter = [[NSDateFormatter alloc] init];
 [forMatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
 NSString *timeDateStr = @"被比较的时间";
 
 // 将两个时间都转化为date
 NSDate *data = [forMatter dateFromString:currentDateStr];
 NSDate *endTime = [forMatter dateFromString: timeDateStr];
 NSComparisonResult result = [data compare:endTime];
 if (result == NSOrderedDescending) {
         // 说明超过比较时间
} else if (result == NSOrderedAscending){
        // 未超过比较时间
} else {
        // 等于比较时间

}

To Be Continued...

相关文章

网友评论

      本文标题:iOS中时间日期的比较

      本文链接:https://www.haomeiwen.com/subject/gpwebttx.html