//日期1 设置为今天
NSDate* nowDate = [NSDate date];
//日期二 这里是我传入的一个时间,自行修改
? ? NSDate * msgDate = [NSDate dateWithTimeIntervalSince1970:msglastTime];
//设置日历单元,注意这个地方,后面自己要用到哪些参数就要设置进去
如NSCalendarUnitYear 是年,NSCalendarUnitDay是日等
? ? NSCalendarUnit components = (NSCalendarUnit)(NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitWeekday|NSCalendarUnitWeekOfMonth|NSCalendarUnitHour | NSCalendarUnitMinute);
//获取第一个时间的日历单元数据
? ? NSDateComponents *nowDateComponents = [[NSCalendar currentCalendar] components:components fromDate:nowDate];
//获取第二个时间的日历单元数据
? ? NSDateComponents *msgDateComponents = [[NSCalendar currentCalendar] components:components fromDate:msgDate];
//后面的比较通过两个日期的单元数据进行比较
? ? NSIntegerhour = msgDateComponents.hour;
? ? doubleOnedayTimeIntervalValue =24*60*60;? //一天的秒数
? ? //判断是否是同一月
? ? BOOLisSameMonth = (nowDateComponents.year== msgDateComponents.year) && (nowDateComponents.month== msgDateComponents.month);
? ? //同一周
? ? NSIntegerweekk = nowDateComponents.weekOfMonth;
? ? NSIntegerweekk2 = msgDateComponents.weekOfMonth;
? ? BOOL?isSameWeek = (nowDateComponents.year== msgDateComponents.year) && (nowDateComponents.month== msgDateComponents.month)&&(nowDateComponents.weekOfMonth== msgDateComponents.weekOfMonth);
? ? //同天
? ? BOOL?isSameDay = isSameMonth && (nowDateComponents.day== msgDateComponents.day);
? ? //昨天
? ? BOOL?isYearsterday =NO;
? ? if(isSameWeek) {
? ? ? ? if(nowDateComponents.day== msgDateComponents.day+1) {
? ? ? ? ? ? isYearsterday =YES;
? ? ? ? }
? ? }