项目中,我们经常会遇到一些时间需要处理的问题,本人最近一个项目就遇到了此类问题,需求要求对一个公告列表进行时间显示处理,当两条连续的时间不超过五分钟时,不显示时间,时间为今天的显示今天,为昨天的显示时间为昨天,一星期以内时间显示为周几,具体代码如下:
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interfaceNSDate (Extension)
//年月日时间数组处理
-(NSMutableArray *)setTimeArray:(NSMutableArray *)array;
//年月日时间字符串单个处理
-(NSString*)setTimeString:(NSString*)string;
@end
NS_ASSUME_NONNULL_END
在.m文件进行逻辑处理:
#import "NSDate+Extension.h"
@implementationNSDate (Extension)
-(NSString*)setTimeString:(NSString*)string{
NSString*nowTime = [selfgetTimeStrWithString:string];
return [self timeStringWithTimeInterval:nowTime];
}
-(NSMutableArray *)setTimeArray:(NSMutableArray *)array{
NSMutableArray *arrayTime = [[NSMutableArray alloc]init];
if(array.count==1) {
[arrayTimeaddObject:array[0]];
}else{
for(inti =0; i < array.count-1; i++) {
if(i ==0) {
[arrayTimeaddObject:array[0]];
}
if([selfvalidateWithStartTime:array[i]withExprireTime:array[i+1]]) {
[arrayTimeaddObject:@""];
}else{
[arrayTimeaddObject:array[i+1]];
}
}
}
for(inti =0; i < arrayTime.count; i ++) {
if(![arrayTime[i]isEqualToString:@""]) {
NSString*string = [selfgetTimeStrWithString:arrayTime[i]];
arrayTime[i] = [selftimeStringWithTimeInterval:string];
NSLog(@"%@",arrayTime[i]);
}
}
returnarrayTime;
}
//判断俩时间是否超过五分钟
-(BOOL)validateWithStartTime:(NSString*)startTime withExprireTime:(NSString*)expireTime{
NSString*nowTime = [selfgetTimeStrWithString:startTime];
NSString*endTime = [selfgetTimeStrWithString:expireTime];
NSInteger i = [endTime integerValue] - [nowTime integerValue];
if(i <300) {
returnYES;
}
return NO;
}
//字符串转时间戳 如:2017-4-10 17:15:10
- (NSString*)getTimeStrWithString:(NSString*)str{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];// 创建一个时间格式化对象
[dateFormattersetDateFormat:@"YYYY-MM-dd HH:mm:ss"];//设定时间的格式
NSDate*tempDate = [dateFormatterdateFromString:str];//将字符串转换为时间对象
NSString *timeStr = [NSString stringWithFormat:@"%ld", (long)[tempDate timeIntervalSince1970]];//字符串转成时间戳,精确到毫秒*1000
returntimeStr;
}
//判断时间戳是否为当天,昨天,一周内,年月日
- (NSString*)timeStringWithTimeInterval:(NSString*)timeInterval{
if([timeIntervalisEqualToString:@""]) {
return@"";
}
NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeInterval.longLongValue]; //此处根据项目需求,选择是否除以1000 , 如果时间戳精确到秒则去掉1000
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
//今天
if([dateisToday]) {
formatter.dateFormat=@"HH:mm";
return[formatterstringFromDate:date];
}else{
//昨天
if([dateisYesterday]) {
formatter.dateFormat=@"昨天 HH:mm";
return[formatterstringFromDate:date];
//一周内 [date weekdayStringFromDate]
}elseif([dateisSameWeek]){
formatter.dateFormat= [NSStringstringWithFormat:@"%@ %@",[dateweekdayStringFromDate],@"HH:mm"];
return[formatterstringFromDate:date];
//直接显示年月日
}else{
formatter.dateFormat=@"yyyy/MM/dd HH:mm";
return[formatterstringFromDate:date];
}
}
return nil;
}
//是否为今天
- (BOOL)isToday{
//now: 2015-09-05 11:23:00
//self 调用这个方法的对象本身
NSCalendar *calendar = [NSCalendar currentCalendar];
int unit = NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear ;
//1.获得当前时间的 年月日
NSDateComponents *nowCmps = [calendar components:unit fromDate:[NSDate date]];
//2.获得self
NSDateComponents*selfCmps = [calendarcomponents:unitfromDate:self];
return(selfCmps.year== nowCmps.year) && (selfCmps.month== nowCmps.month) && (selfCmps.day== nowCmps.day);
}
//是否为昨天
- (BOOL)isYesterday{
//2014-05-01
NSDate*nowDate = [[NSDatedate]dateWithYMD];
//2014-04-30
NSDate*selfDate = [selfdateWithYMD];
//获得nowDate和selfDate的差距
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *cmps = [calendar components:NSCalendarUnitDay fromDate:selfDate toDate:nowDate options:0];
returncmps.day==1;
}
//是否在同一周
- (BOOL)isSameWeek{
NSCalendar *calendar = [NSCalendar currentCalendar];
int unit = NSCalendarUnitWeekday | NSCalendarUnitMonth | NSCalendarUnitYear ;
//1.获得当前时间的 年月日
NSDateComponents *nowCmps = [calendar components:unit fromDate:[NSDate date]];
//2.获得self
NSDateComponents*selfCmps = [calendarcomponents:unitfromDate:self];
return(selfCmps.year== nowCmps.year) && (selfCmps.month== nowCmps.month) && (selfCmps.day== nowCmps.day);
}
//根据日期求星期几
- (NSString*)weekdayStringFromDate{
NSArray *weekdays = [NSArray arrayWithObjects: [NSNull null], @"星期天", @"星期一", @"星期二", @"星期三", @"星期四", @"星期五", @"星期六", nil];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/Shanghai"];
[calendarsetTimeZone: timeZone];
NSCalendarUnit calendarUnit = NSCalendarUnitWeekday; NSDateComponents*theComponents = [calendarcomponents:calendarUnitfromDate:self];
return[weekdaysobjectAtIndex:theComponents.weekday];
}
//格式化
- (NSDate*)dateWithYMD{
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
fmt.dateFormat = @"yyyy-MM-dd";
NSString*selfStr = [fmtstringFromDate:self];
return[fmtdateFromString:selfStr];
}