- (NSDateComponents *)componentsOfDay {
return [[NSCalendar currentCalendar]components:NSCalendarUnitYear | NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitWeekday|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond fromDate: self];
}
- (BOOL)isEqualToDateIgnorTime:(NSDate *)date
{
NSDateComponents *components = [self componentsOfDay];
NSDateComponents *componentd = [date componentsOfDay];
return (componentd.year == components.year && components.month == componentd.month && components.day == componentd.day);
}
- (NSString *)stringWithExcludeThisYear {
BOOL isThisYear =NO;
NSDate *nowDate = [NSDate date];
if ([nowDate componentsOfDay].year != [self componentsOfDay].year) {
isThisYear =NO;
}else{
isThisYear =YES;
}
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
if (isThisYear) {
[formatter setDateFormat:@"MM月dd日"];
}else{
[formatter setDateFormat:@"yyyy年MM月dd日"];
}
return [formatter stringFromDate:self];
}
//判断是否是今天
- (BOOL)isToday {
NSDate *date = [NSDate date];
return [self isEqualToDateIgnorTime:date];
}
//判断是否是昨天
- (BOOL)isYesterDay {
NSTimeInterval time = [[NSDate date]timeIntervalSince1970]-24*60*60;
NSDate *yesterDayDate = [NSDate dateWithTimeIntervalSince1970:time];
return [self isEqualToDateIgnorTime:yesterDayDate];
}
+(NSString *)stringWithTimeInterval:(NSTimeInterval)time {
NSDate *date = [NSDate dateWithTimeIntervalSince1970:time];
NSInteger hour = [date componentsOfDay].hour;
NSInteger minute = [date componentsOfDay].minute;
NSTimeInterval nowTime = [[NSDate date]timeIntervalSince1970];
if (nowTime - time>=7*24*60*60) {
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@"yyyy年MM月dd日 HH:mm"];
return [formatter stringFromDate:date];
}else{
if ([date isToday]) {
return [NSString stringWithFormat:@"今天 %ld:%ld",hour,minute];
}else if ([date isYesterDay]){
return [NSString stringWithFormat:@"昨天 %ld:%ld",hour,minute];
}else{
NSArray *weekInfo = @[@"星期日",@"星期一",@"星期二",@"星期三",@"星期四",@"星期五",@"星期六"];
NSInteger weekDay = [date componentsOfDay].weekday;
NSString *weekString = weekInfo[weekDay -1];
return [NSString stringWithFormat:@"%@ %ld:%ld",weekString, hour,minute];
}
}
}


被折叠的 条评论
为什么被折叠?



