/*******
設(shè)置兩個(gè)時(shí)間差為固定值
*******/
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDate *currentDate = [NSDate date];
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setDay:1];//設(shè)置最大時(shí)間為:當(dāng)前時(shí)間推后1天
NSDate *maxDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];
NSDateFormatter *formater = [[NSDateFormatter alloc] init];
formater.dateFormat =@"yyyy-MM-dd HH:mm";
/*******
日期可以進(jìn)行比較以確定大小或相等浇衬,也可以確定兩個(gè)日期之間的時(shí)間間隔虏束。兩個(gè)日期的間隔時(shí)間差可以使用-timeIntervalSinceDate:方法來計(jì)算
*******/
NSDate* now = [NSDatedate];
NSDate* anHourAgo = [nowdateByAddingTimeInterval:-60*60];
NSTimeIntervaltimeBetween = [nowtimeIntervalSinceDate:anHourAgo];
NSLog(@"%f",timeBetween);//結(jié)果:3600.000000
/*******
日期比較也可以使用-timeIntervalSinceNow方法獲取和當(dāng)前的時(shí)間間隔
*********/
NSDate* anHourago = [NSDatedateWithTimeIntervalSinceNow:-60*60];
NSTimeIntervalinterval = [anHouragotimeIntervalSinceNow];
NSLog(@"%f",interval);//結(jié)果:-3600.000007
/****
iOS比較日期大小默認(rèn)會(huì)比較到秒
****/
+(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;
}
/************
日期格式請(qǐng)傳入: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;
}
/*********
比較兩個(gè)日期之間的差值
*******/
+ (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];
//如果是真機(jī)調(diào)試,轉(zhuǎn)換這種歐美時(shí)間捷枯,需要設(shè)置locale
fmt.locale= [[NSLocalealloc]initWithLocaleIdentifier:@"en_US"];
//設(shè)置日期格式(聲明字符串里面每個(gè)數(shù)字和單詞的含義)
// E:星期幾
// M:月份
// d:幾號(hào)(這個(gè)月的第幾天)
// H:24小時(shí)制的小時(shí)
// m:分鐘
// s:秒
// y:年
fmt.dateFormat=@"EEE MMM dd HH:mm:ss Z yyyy";
//微博的創(chuàng)建日期
NSDate*createDate = [fmtdateFromString:dateString];
//當(dāng)前時(shí)間
NSDate*now = [NSDatedate];
//日歷對(duì)象(方便比較兩個(gè)日期之間的差距)
NSCalendar*calendar = [NSCalendarcurrentCalendar];
// NSCalendarUnit枚舉代表想獲得哪些差值
NSCalendarUnitunit =NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond;
//計(jì)算兩個(gè)日期之間的差值
NSDateComponents*cmps = [calendarcomponents:unitfromDate:createDatetoDate:nowoptions:0];
NSLog(@"%@ %@ %@", createDate, now, cmps);
returncmps;
}
/*********
比較兩個(gè)日期之間某年或某月或某日或某時(shí)等的具體差值
*******/
+ (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;//年崇败、月、日歌焦、時(shí)飞几、分、秒独撇、周等等都可以
NSDateComponents*comps = [gregoriancomponents:unitFlagsfromDate:date1toDate:date2options:0];
intmonth = [compsmonth];//時(shí)間差
NSLog(@"時(shí)間差= %d屑墨,abs(month)=%d",month,abs(month));
returnmonth;
}
由NSDate轉(zhuǎn)換為NSString:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *strDate = [dateFormatter stringFromDate:[NSDate date]];
NSLog(@"%@", strDate);
[dateFormatter release];
結(jié)果:
2010-08-04 16:01:03
由NSString轉(zhuǎn)換為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];
結(jié)果:
2010-08-04 16:01:03 +0800
日期之間比較可用以下方法
- (BOOL)isEqualToDate:(NSDate *)otherDate;
與otherDate比較躁锁,相同返回YES
- (NSDate *)earlierDate:(NSDate *)anotherDate;
與anotherDate比較,返回較早的那個(gè)日期
- (NSDate *)laterDate:(NSDate *)anotherDate;
與anotherDate比較卵史,返回較晚的那個(gè)日期
- (NSComparisonResult)compare:(NSDate *)other;
該方法用于排序時(shí)調(diào)用:
.當(dāng)實(shí)例保存的日期值與anotherDate相同時(shí)返回NSOrderedSame
.當(dāng)實(shí)例保存的日期值晚于anotherDate時(shí)返回NSOrderedDescending
.當(dāng)實(shí)例保存的日期值早于anotherDate時(shí)返回NSOrderedAscending