时间日期判断实例
直接看代码吧:
// Created by cctv on 15/8/7. // Copyright (c) 2015年 cctv. All rights reserved. //
#import "NSString+add.h" #import "NSDate+add.h"
@implementation NSString (add) + (NSString *)stringWithDateString:(NSString*) dateString { NSDateFormatter *dateF = [[NSDateFormatter alloc] init]; dateF.dateFormat = @"EEE MMM dd HH:mm:ss Z yyyy"; dateF.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en-AS"]; [dateF dateFromString:dateString]; // 创建时间 NSDate *date = [dateF dateFromString:dateString]; //判断创建时间与当前时间的关系 if (date.dateIsThisDay) { NSDateComponents *dateComp = [date dateIsToday]; if (dateComp.hour >= 1) { dateF.dateFormat = [NSString stringWithFormat:@"%d小时前",dateComp.hour]; return [dateF stringFromDate:date]; }else if(dateComp.minute >= 1 ){ dateF.dateFormat = [NSString stringWithFormat:@"%d分钟前",dateComp.minute]; return [dateF stringFromDate:date]; }else{ return @"刚刚"; } }else if (date.dateIsYesterDay){ dateF.dateFormat = @"昨天 HH:mm"; return [dateF stringFromDate:date]; }else if(date.dateIsThisYear){ dateF.dateFormat = @"MM-dd HH:mm"; return [dateF stringFromDate:date]; }else{ dateF.dateFormat = @"yyyy-MM-dd HH:mm"; return [dateF stringFromDate:date]; } }
@end --------。。。。。------
#import "NSDate+add.h"
@implementation NSDate (add) - (BOOL)dateIsThisYear { NSCalendar *calendar = [NSCalendar currentCalendar]; NSDateComponents *nowComp = [calendar components:(NSCalendarUnitYear) fromDate:[NSDate date]]; NSDateComponents *selfComp = [calendar components:(NSCalendarUnitYear) fromDate:self]; return nowComp.year == selfComp.year; } - (BOOL)dateIsThisDay { NSCalendar *calendar = [NSCalendar currentCalendar]; NSDateComponents *nowComp = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:[NSDate date]]; NSDateComponents *selfComp = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:self]; return nowComp.year == selfComp.year && nowComp.month == selfComp.month && nowComp.day == selfComp.day; } - (BOOL)dateIsYesterDay { NSCalendar *calendar = [NSCalendar currentCalendar]; // 当前时间 NSDateComponents *now = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:[NSDate date]]; now.hour = 0; now.minute = 0; now.second =0; NSDate *nowTime = [calendar dateFromComponents:now]; // 需要比较的时间 NSDateComponents *isMe = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:self]; isMe.hour = 0; isMe.minute = 0; isMe.second =0; NSDate *isMeTime = [calendar dateFromComponents:isMe]; NSDateComponents *cctv = [calendar components:NSCalendarUnitDay fromDate:isMeTime toDate:nowTime options:0]; if (cctv.Year == 0 && cctv.Month == 0 && cctv.day == 1) { return YES; }else { return NO; }
} - (NSDateComponents*)dateIsToday { NSCalendar *calendar = [NSCalendar currentCalendar]; NSDateComponents *dateComp = [calendar components:NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond fromDate:self toDate:[NSDate date] options:0]; return dateComp; }
@end
|