/*******
日期可以进行比较以确定大小或相等,也可以确定两个日期之间的时间间隔。两个日期的间隔时间差可以使用-timeIntervalSinceDate:方法来计算
*******/
NSDate * now = [NSDatedate];
NSDate * anHourAgo = [nowdateByAddingTimeInterval:-60*60];
NSTimeInterval timeBetween = [nowtimeIntervalSinceDate:anHourAgo];
NSLog(@"%f",timeBetween);//结果:3600.000000
/*******
日期比较也可以使用-timeIntervalSinceNow方法获取和当前的时间间隔
*********/
NSDate * anHourago = [NSDatedateWithTimeIntervalSinceNow:-60*60];
NSTimeInterval interval = [anHouragotimeIntervalSinceNow];
NSLog(@"%f",interval);//结果: -3600.000007
/****
iOS比较日期大小默认会比较到秒
****/
+(int)compareOneDay:(NSDate *)oneDay withAnotherDay:(NSDate *)anotherDay
{
NSDateFormatter *dateFormatter = [[NSDateFormatteralloc] 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;
}
/************
日期格式请传入: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{
int ci;
NSDateFormatter *df = [[NSDateFormatteralloc] init];
[df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *dt1 = [[NSDatealloc] init];
NSDate *dt2 = [[NSDatealloc] init];
dt1 = [df dateFromString:date01];
dt2 = [df dateFromString:date02];
NSComparisonResult result = [dt1 compare:dt2];
switch (result)
{
//date02比date01大
caseNSOrderedAscending: ci=1;break;
//date02比date01小
caseNSOrderedDescending: ci=-1;break;
//date02=date01
case NSOrderedSame: ci=0;break;
default: NSLog(@"erorr dates %@, %@", dt2, dt1);break;
}
return ci;
}
/*********
比较两个日期之间的差值
*******/
+ (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 = [fmt dateFromString:dateString];
// 当前时间
NSDate *now = [NSDatedate];
// 日历对象(方便比较两个日期之间的差距)
NSCalendar *calendar = [NSCalendarcurrentCalendar];
// NSCalendarUnit枚举代表想获得哪些差值
NSCalendarUnit unit =NSCalendarUnitYear | NSCalendarUnitMonth |NSCalendarUnitDay | NSCalendarUnitHour |NSCalendarUnitMinute | NSCalendarUnitSecond;
// 计算两个日期之间的差值
NSDateComponents *cmps = [calendar components:unit fromDate:createDate toDate:now options:0];
NSLog(@"%@ %@ %@", createDate, now, cmps);
return cmps;
}
/*********
比较两个日期之间某年或某月或某日或某时等的具体差值
*******/
+ (instancetype)differencewithDate:(NSString*)dateString withDate:(NSString*)anotherdateString{
{
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
// [formatter setDateFormat: @"yyyy-MM-dd HH:mm:ss"];
[formatter setDateFormat: @"yyyy-MM-dd "];
NSDate *date2 = [formatter dateFromString :dateString];
NSDate *date1 = [formatter dateFromString :anotherdateString];
// NSLog(@"------date1=%@ date2=%@------%@",date1, date2);
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
unsigned int unitFlags = NSMonthCalendarUnit;//年、月、日、时、分、秒、周等等都可以
NSDateComponents *comps = [gregorian components:unitFlags fromDate:date1 toDate:date2 options:0];
int month = [comps month];//时间差
NSLog(@"时间差 = %d,abs(month)=%d",month,abs(month));
return month;
}