最近有个需求处理时间格式化如下图
时间格式化效果.png
当天显示为 今天 和 HH:mm, 不是同一天的显示为 MM/dd 和 HH:mm, 非当年的显示yyyy年
最后用了一个简单的思路来解决下
先写一个工具类
.h文件
@interface TimeModel : NSObject
@property (nonatomic, copy) NSString *yearString;
@property (nonatomic, copy) NSString *monthDayString;
@property (nonatomic, copy) NSString *timeString;
/*
* 时间格式化 当天显示为 今天 和 HH:mm, 非当天显示MM/dd 和 HH:mm, 非当年的显示yyyy年
*pragma prevDate - 上一个时间
*pragma nextDate - 当前需要计算的时间
*pragma formate - 时间格式化样式
*/
+ (TimeModel *)formateDate:(NSDate *)prevDate
nextDate:(NSDate *)nextDate
withFormate:(NSString *)formate;
@end
.m文件
@implementation TimeModel
/*
* 时间格式化 当天显示为 今天 和 HH:mm, 非当天显示MM/dd 和 HH:mm, 非当年的显示yyyy年
*pragma prevDate - 上一个时间
*pragma nextDate - 当前需要计算的时间
*pragma formate - 时间格式化样式
*/
+ (TimeModel *)formateDate:(NSDate *)prevDate
nextDate:(NSDate *)nextDate
withFormate:(NSString *)formate
{
@try {
if (formate.length < 0) {
formate = @"yyyy-MM-dd HH:mm:ss";
}
//实例化一个NSDateFormatter对象
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:formate];
NSDate * nowDate = [NSDate date];
// 再然后,把间隔的秒数折算成天数和小时数:
NSString *yearString = @"";
NSString *monthDayString = @"";
NSString *timeString = @"";
/** 同一天的数据 */
[dateFormatter setDateFormat:@"MM/dd"];
NSString *prevMonthDay = [dateFormatter stringFromDate:prevDate];
NSString *needMonthDay = [dateFormatter stringFromDate:nextDate];
NSString *nowMonthDay = [dateFormatter stringFromDate:nowDate];
/** 同一年 */
[dateFormatter setDateFormat:@"yyyy年"];
NSString *prevYear = [dateFormatter stringFromDate:prevDate];
NSString *needYear = [dateFormatter stringFromDate:nextDate];
NSString *nowYear = [dateFormatter stringFromDate:nowDate];
/** 处理显示年 */
if (![needYear isEqualToString:prevYear]) {
yearString = needYear;
if (!prevYear && [needYear isEqualToString:nowYear]) {
yearString = @"";
}
}
/** 处理显示天 */
if (![needMonthDay isEqualToString:prevMonthDay]) {
monthDayString = needMonthDay;
[dateFormatter setDateFormat:@"HH:mm"];
timeString = [dateFormatter stringFromDate:nextDate];
if (!prevMonthDay) {
if ([needMonthDay isEqualToString:nowMonthDay]) {
monthDayString = @"今天";
}
}
} else {
[dateFormatter setDateFormat:@"HH:mm"];
timeString = [dateFormatter stringFromDate:nextDate];
}
TimeModel *timeModel = [[TimeModel alloc] init];
timeModel.yearString = yearString;
timeModel.monthDayString = monthDayString;
timeModel.timeString = timeString;
return timeModel;
} @catch (NSException *exception) {
TimeModel *timeModel = [[TimeModel alloc] init];
return timeModel;
}
}
@end
实际中的业务逻辑是可以获取到一些时间数组,以时间戳为例
//获取当前时间戳 (以毫秒为单位)
+ (NSString *)getNowTimeTimestamp3{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ;
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss SSS"]; // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
//设置时区,这个对于时间的处理有时很重要
NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
[formatter setTimeZone:timeZone];
NSDate *datenow = [NSDate date];//现在时间,你可以输出来看下是什么格式
NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[datenow timeIntervalSince1970]*1000];
return timeSp;
}
/** 存储时间戳数组 */
- (NSMutableArray *)dataArray
{
if (!_dataArray) {
_dataArray = [NSMutableArray array];
[_dataArray addObject:[[self class] getNowTimeTimestamp3]];
[_dataArray addObject:@"1634300370000"];
[_dataArray addObject:@"1634806363000"];
[_dataArray addObject:@"1634806422000"];
}
return _dataArray;
}
/** 存储格式化后的 TimeModel 数据 */
- (NSMutableArray *)resultArray
{
if (!_resultArray) {
_resultArray = [NSMutableArray array];
}
return _resultArray;
}
/**
将时间戳转化为date类型
@param dateString 毫秒字符串时间
@return NSDate日期
*/
- (NSDate *)formatDateWithDateString:(NSString *)dateString
{
NSTimeInterval time = [dateString longLongValue]/1000;
NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:time];
return confromTimesp;
}
暂时写在touch事件里做测试
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[self.resultArray removeAllObjects];
for (int i = 0; i < self.dataArray.count; i++) {
NSDate *infoDate;
NSDate *nextDate;
if (i == 0) {
infoDate = nil;
}else{
infoDate = [self formatDateWithDateString:self.dataArray[i-1]];
}
nextDate = [self formatDateWithDateString:self.dataArray[i]];
TimeModel *timeModel = [[self class] formateDate:infoDate nextDate:nextDate withFormate:@""];
[self.resultArray addObject:timeModel];
}
NSLog(@"self.resultArray = %@", self.resultArray);
}
时间处理结束
网友评论