/**
* 设置 天数
*/
- (void)setTimesLabelWithTime:(NSString*)strTime
{
//计算
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
NSDate *datePKTime = [formatter dateFromString:strTime];
NSDate *dateNow = [NSDate date];
NSString *strToday = [formatter stringFromDate:[NSDate date]];
NSInteger dayN = ([dateNow timeIntervalSince1970] - [datePKTime timeIntervalSince1970])/(3600*24);
BOOL today = [[strTime substringToIndex:10] isEqualToString:[strToday substringToIndex:10]];
if (today) {
[_timeLabel setText:@"今天"];
} else {
if (dayN <= 1) {
[_timeLabel setText:@"1天前"];
} else if (dayN >1 && dayN <= 7) {
[_timeLabel setText:[NSString stringWithFormat:@"%tu天前",dayN]];
} else {
BOOL thisYear = [[strTime substringToIndex:4] isEqualToString:[strToday substringToIndex:4]];
if (thisYear) {
[_timeLabel setText:[strTime substringWithRange:NSMakeRange(5, 5)]];
}else {
[_timeLabel setText:[strTime substringToIndex:10]];
}
}
}
}
本文介绍了一个iOS应用中处理日期和时间的方法,通过Objective-C代码实现,可以将特定的时间戳转换为易于理解的时间表达形式,例如“今天”、“几天前”等,并能根据不同情况进行适配显示。
1290

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



