iOS 时间比较大小

/*******

设置两个时间差为固定值

*******/

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

NSDate *currentDate = [NSDate date];

NSDateComponents *comps = [[NSDateComponents alloc] init];

[comps setDay:1];//设置最大时间为:当前时间推后1天

NSDate *maxDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];

NSDateFormatter *formater = [[NSDateFormatter alloc] init];

formater.dateFormat =@"yyyy-MM-dd HH:mm";

/*******

日期可以进行比较以确定大小或相等,也可以确定两个日期之间的时间间隔。两个日期的间隔时间差可以使用-timeIntervalSinceDate:方法来计算

*******/

NSDate* now = [NSDatedate];

NSDate* anHourAgo = [nowdateByAddingTimeInterval:-60*60];

NSTimeIntervaltimeBetween = [nowtimeIntervalSinceDate:anHourAgo];

NSLog(@"%f",timeBetween);//结果:3600.000000

/*******

日期比较也可以使用-timeIntervalSinceNow方法获取和当前的时间间隔

*********/

NSDate* anHourago = [NSDatedateWithTimeIntervalSinceNow:-60*60];

NSTimeIntervalinterval = [anHouragotimeIntervalSinceNow];

NSLog(@"%f",interval);//结果:-3600.000007

/****

iOS比较日期大小默认会比较到秒

****/

+(int)compareOneDay:(NSDate*)oneDay withAnotherDay:(NSDate*)anotherDay

{

NSDateFormatter*dateFormatter = [[NSDateFormatteralloc]init];

[dateFormattersetDateFormat:@"dd-MM-yyyy"];

NSString*oneDayStr = [dateFormatterstringFromDate:oneDay];

NSString*anotherDayStr = [dateFormatterstringFromDate:anotherDay];

NSDate*dateA = [dateFormatterdateFromString:oneDayStr];

NSDate*dateB = [dateFormatterdateFromString:anotherDayStr];

NSComparisonResultresult = [dateAcompare:dateB];

NSLog(@"date1 : %@, date2 : %@", oneDay, anotherDay);

if(result ==NSOrderedDescending) {

//NSLog(@"Date1  is in the future");

return1;

}

elseif(result ==NSOrderedAscending){

//NSLog(@"Date1 is in the past");

return-1;

}

//NSLog(@"Both dates are the same");

return0;

}

/************

日期格式请传入:2013-08-05 12:12:12;如果修改日期格式,比如:2013-08-05,则将[df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];修改为[df setDateFormat:@"yyyy-MM-dd"];

***********/

+(int)compareDate:(NSString*)date01 withDate:(NSString*)date02{

intci;

NSDateFormatter*df = [[NSDateFormatteralloc]init];

[dfsetDateFormat:@"yyyy-MM-dd HH:mm:ss"];

NSDate*dt1 = [[NSDatealloc]init];

NSDate*dt2 = [[NSDatealloc]init];

dt1 = [dfdateFromString:date01];

dt2 = [dfdateFromString:date02];

NSComparisonResultresult = [dt1compare:dt2];

switch(result)

{

//date02比date01大

caseNSOrderedAscending: ci=1;break;

//date02比date01小

caseNSOrderedDescending: ci=-1;break;

//date02=date01

caseNSOrderedSame: ci=0;break;

default:NSLog(@"erorr dates %@, %@", dt2, dt1);break;

}

returnci;

}

/*********

比较两个日期之间的差值

*******/

+ (instancetype)differencewithDate:(NSString*)dateString withDate:(NSString*)anotherdateString{

{

// _created_at == Thu Oct 16 17:06:25 +0800 2014

// dateFormat == EEE MMM dd HH:mm:ss Z yyyy

// NSString --> NSDate

NSDateFormatter*fmt = [[NSDateFormatteralloc]init];

//如果是真机调试,转换这种欧美时间,需要设置locale

fmt.locale= [[NSLocalealloc]initWithLocaleIdentifier:@"en_US"];

//设置日期格式(声明字符串里面每个数字和单词的含义)

// E:星期几

// M:月份

// d:几号(这个月的第几天)

// H:24小时制的小时

// m:分钟

// s:秒

// y:年

fmt.dateFormat=@"EEE MMM dd HH:mm:ss Z yyyy";

//微博的创建日期

NSDate*createDate = [fmtdateFromString:dateString];

//当前时间

NSDate*now = [NSDatedate];

//日历对象(方便比较两个日期之间的差距)

NSCalendar*calendar = [NSCalendarcurrentCalendar];

// NSCalendarUnit枚举代表想获得哪些差值

NSCalendarUnitunit =NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond;

//计算两个日期之间的差值

NSDateComponents*cmps = [calendarcomponents:unitfromDate:createDatetoDate:nowoptions:0];

NSLog(@"%@ %@ %@", createDate, now, cmps);

returncmps;

}

/*********

比较两个日期之间某年或某月或某日或某时等的具体差值

*******/

+ (instancetype)differencewithDate:(NSString*)dateString withDate:(NSString*)anotherdateString{

{

NSDateFormatter* formatter = [[NSDateFormatteralloc]init];

//    [formatter setDateFormat: @"yyyy-MM-dd HH:mm:ss"];

[formattersetDateFormat:@"yyyy-MM-dd "];

NSDate*date2 = [formatterdateFromString:dateString];

NSDate*date1 = [formatterdateFromString:anotherdateString];

//     NSLog(@"------date1=%@   date2=%@------%@",date1, date2);

NSCalendar*gregorian = [[NSCalendaralloc]initWithCalendarIdentifier:NSGregorianCalendar];

unsignedintunitFlags =NSMonthCalendarUnit;//年、月、日、时、分、秒、周等等都可以

NSDateComponents*comps = [gregoriancomponents:unitFlagsfromDate:date1toDate:date2options:0];

intmonth = [compsmonth];//时间差

NSLog(@"时间差= %d,abs(month)=%d",month,abs(month));

returnmonth;

}

由NSDate转换为NSString:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

NSString *strDate = [dateFormatter stringFromDate:[NSDate date]];

NSLog(@"%@", strDate);

[dateFormatter release];

结果:

2010-08-04 16:01:03

由NSString转换为NSDate:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

NSDate *date = [dateFormatter dateFromString:@"2010-08-04 16:01:03"];

NSLog(@"%@", date);

[dateFormatter release];

结果:

2010-08-04 16:01:03 +0800

日期之间比较可用以下方法

- (BOOL)isEqualToDate:(NSDate *)otherDate;

与otherDate比较,相同返回YES

- (NSDate *)earlierDate:(NSDate *)anotherDate;

与anotherDate比较,返回较早的那个日期

- (NSDate *)laterDate:(NSDate *)anotherDate;

与anotherDate比较,返回较晚的那个日期

- (NSComparisonResult)compare:(NSDate *)other;

该方法用于排序时调用:

.当实例保存的日期值与anotherDate相同时返回NSOrderedSame

.当实例保存的日期值晚于anotherDate时返回NSOrderedDescending

.当实例保存的日期值早于anotherDate时返回NSOrderedAscending

你可能感兴趣的:(iOS 时间比较大小)