iOS编程中比较两个日期的大小
2015-03-20 17:16
288 查看
转自:http://www.myext.cn/other/a_30922.html
比较日期大小是任何编程语言都会经常遇到的问题,再iOS编程中,通常用NSDate对象来存储一个时间(包括日期和时间、时区),而且 NSDate类提供了compare方法来进行时间的比较,但有时不想那么精确的知道两个日期的大小(默认会比较到秒),可以用下面的实现方法:
比较日期大小是任何编程语言都会经常遇到的问题,再iOS编程中,通常用NSDate对象来存储一个时间(包括日期和时间、时区),而且 NSDate类提供了compare方法来进行时间的比较,但有时不想那么精确的知道两个日期的大小(默认会比较到秒),可以用下面的实现方法:
+(int)compareOneDay:(NSDate *)oneDay withAnotherDay:(NSDate *)anotherDay { NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"dd-MM-yyyy"]; 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"); return 0; }
相关文章推荐
- iOS编程中比较两个日期的大小
- iOS编程中比较两个日期的大小
- php实现比较两个字符串日期大小的方法
- PHP中比较两个时间的大小与日期的差值
- C#比较两个日期的大小
- java中比较两个日期Date的大小
- JS 比较两个日期大小
- js比较两个"日期时间"的大小
- JavaScript 比较两个日期大小 及格式化时间函数 java获取当前日期前几天或后几天
- Function-两个日期大小比较
- C#比较两个日期的大小两种案例解析
- 比较两个日期的大小 NSDate获取当前日期 进行比较
- JS比较两个日期大小
- java 比较两个日期大小
- DateTime.Compare(t1,t2)比较两个日期大小
- PHP中比较两个时间的大小与日期的差值
- js比较两个日期大小
- java实现计算两个日期相差多少月、比较两个日期大小 等常用日期操作
- js-比较两个日期的大小