• iOS比较当前日期与指定日期大小


        #pragma mark -得到当前时间  
        - (NSDate *)getCurrentTime{  
            NSDateFormatter *formatter=[[NSDateFormatter alloc]init];  
            [formatter setDateFormat:@"dd-MM-yyyy-HHmmss"];  
            NSString *dateTime=[formatter stringFromDate:[NSDate date]];  
            NSDate *date = [formatter dateFromString:dateTime];  
              
            NSLog(@"---------- currentDate == %@",date);  
            return date;  
        }  

    将指定日期(此处以2016-09-30-00点为例)转换为同样日期格式,返回NSDate形式

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];      
    [dateFormatter setDateFormat:@"dd-MM-yyyy-HHmmss"];  
    NSDate *date = [dateFormatter dateFromString:@"30-09-2016-000000"];  

    将现在的时间与指定时间比较,如果没达到指定日期,返回-1,刚好是这一时间,返回0,否则返回1

        - (int)compareOneDay:(NSDate *)oneDay withAnotherDay:(NSDate *)anotherDay  
        {  
            NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];  
            [dateFormatter setDateFormat:@"dd-MM-yyyy-HHmmss"];  
            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;  
              
        }  

    使用

  • 相关阅读:
    03继承与多态 动手动脑
    data whitening
    特征值和特征向量
    CRC算法
    python 二维字典
    协方差的计算与理解
    Differences between write through and write back
    Differences between page and segment
    虚拟内存和缓存区别
    Python: 统计一个文件中每个单词出现的次数
  • 原文地址:https://www.cnblogs.com/ios988/p/7410758.html
一二三 - 开发者的网上家园