/**
中文格式时间处理
@param timeStr 传入的时间串 格式:"2019-07-08 10:05"
@return 返回处理后的时间字符串:周三 16:39
*/
-(NSString *)timeTransformation:(NSString *)timeStr {
NSString *formatStringForHours = [NSDateFormatter dateFormatFromTemplate:@"j" options:0 locale:[NSLocale currentLocale]];
NSRange containsA = [formatStringForHours rangeOfString:@"a"];
BOOL hasAMPM = containsA.location != NSNotFound;
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyy-MM-dd HH:mm";
NSArray *dateArr = [timeStr componentsSeparatedByString:@" "];
NSArray *preDateArr = [[dateArr firstObject] componentsSeparatedByString:@"-"];
NSString *preDateStr;
if (preDateArr.count == 3) {
preDateStr = [NSString stringWithFormat:@"%@年%@月%@日 %@",
preDateArr[0],preDateArr[1],preDateArr[2],dateArr.lastObject];
}
// 聊天时间
NSDate *create = [formatter dateFromString:timeStr];
if (create.isThisYear) { // 今年
if (create.isWithinSevendays) { //是否处于七天内
NSLog(@"七天内");
if (create.isToday) { // 今天
if (hasAMPM) { //12小时制
NSDateComponents *cmps = [create currentTime];
if (cmps.hour<=12){
formatter.AMSymbol = @"上午";
if (cmps.hour<10) {
formatter.dateFormat = @"aaah:mm";
}else{
formatter.dateFormat = @"aaahh:mm";
}
}else {
formatter.PMSymbol = @"下午";
if (cmps.hour%12 <10) {
formatter.dateFormat = @"aaah:mm";
}else{
formatter.dateFormat = @"aaahh:mm";
}
}
// return [formatter stringFromDate:create];
return [NSString stringWithFormat:@"%@ %@",@"今天",[formatter stringFromDate:create]];
}else {
formatter.dateFormat = @"HH:mm";
return [NSString stringWithFormat:@"%@ %@",@"今天",[formatter stringFromDate:create]];//[formatter stringFromDate:create];
}
} else if (create.isYesterday) { // 昨天
if (hasAMPM) { //12小时制
NSDateComponents *cmps = [create currentTime];
if (cmps.hour<=12){
formatter.AMSymbol = @"昨天 上午";
if (cmps.hour<10) {
formatter.dateFormat = @"aaah:mm";
}else{
formatter.dateFormat = @"aaahh:mm";
}
}else {
formatter.PMSymbol = @"昨天 下午";
if (cmps.hour%12 <10) {
formatter.dateFormat = @"aaah:mm";
}else{
formatter.dateFormat = @"aaahh:mm";
}
}
return [formatter stringFromDate:create];
}else {
formatter.dateFormat = @"HH:mm";
return [NSString stringWithFormat:@"%@ %@",@"昨天",[formatter stringFromDate:create]];
}
}else if (create.iSThebeforeday) { // 前天
if (hasAMPM) { //12小时制
NSDateComponents *cmps = [create currentTime];
if (cmps.hour<=12){
formatter.AMSymbol = @"前天 上午";
if (cmps.hour<10) {
formatter.dateFormat = @"aaah:mm";
}else{
formatter.dateFormat = @"aaahh:mm";
}
}else {
formatter.PMSymbol = @"前天 下午";
if (cmps.hour%12 <10) {
formatter.dateFormat = @"aaah:mm";
}else{
formatter.dateFormat = @"aaahh:mm";
}
}
return [formatter stringFromDate:create];
}else {
formatter.dateFormat = @"HH:mm";
return [NSString stringWithFormat:@"%@ %@",@"前天",[formatter stringFromDate:create]];
}
} else if (create.isThisWeak){ //本星期
if (hasAMPM) { //12小时制
NSDateComponents *cmps = [create currentTime];
formatter.weekdaySymbols = @[@"周日",@"周一",@"周二",@"周三",@"周四",@"周五",@"周六"];
if (cmps.hour<=12){
formatter.AMSymbol = @"上午";
if (cmps.hour<10) {
formatter.dateFormat = @"EEEE aaah:mm";
}else{
formatter.dateFormat = @"EEEE aaahh:mm";
}
}else {
formatter.PMSymbol = @"下午";
if (cmps.hour%12 <10) {
formatter.dateFormat = @"EEEE aaah:mm";
}else{
formatter.dateFormat = @"EEEE aaahh:mm";
}
}
return [formatter stringFromDate:create];
}else {
formatter.weekdaySymbols = @[@"周日",@"周一",@"周二",@"周三",@"周四",@"周五",@"周六"];
formatter.dateFormat = @"EEEE HH:mm";
return [formatter stringFromDate:create];
}
}else {
if (hasAMPM) { //12小时制
NSDateComponents *cmps = [create currentTime];
formatter.weekdaySymbols = @[@"周日",@"周一",@"周二",@"周三",@"周四",@"周五",@"周六"];
if (cmps.hour<=12){
formatter.AMSymbol = @"上午";
if (cmps.hour<10) {
formatter.dateFormat = @"EEEE aaah:mm";
}else{
formatter.dateFormat = @"EEEE aaahh:mm";
}
}else {
formatter.PMSymbol = @"下午";
if (cmps.hour%12 <10) {
formatter.dateFormat = @"EEEE aaah:mm";
}else{
formatter.dateFormat = @"EEEE aaahh:mm";
}
}
return [formatter stringFromDate:create];
}else {
formatter.weekdaySymbols = @[@"周日",@"周一",@"周二",@"周三",@"周四",@"周五",@"周六"];
formatter.dateFormat = @"EEEE HH:mm";
return [formatter stringFromDate:create];
}
}
}
else { //今年其他日期
preDateStr = [NSString stringWithFormat:@"%@月%@日",
preDateArr[1],preDateArr[2]];
formatter.weekdaySymbols = @[@"周日",@"周一",@"周二",@"周三",@"周四",@"周五",@"周六"];
if (hasAMPM) { //12小时制
NSDateComponents *cmps = [create currentTime];
if (cmps.hour<=12){
formatter.AMSymbol = @"上午";
if (cmps.hour<10) {
formatter.dateFormat = @"EEEE aaah:mm";
}else{
formatter.dateFormat = @"EEEE aaahh:mm";
}
}else {
formatter.PMSymbol = @"下午";
if (cmps.hour%12 <10) {
formatter.dateFormat = @"EEEE aaah:mm";
}else{
formatter.dateFormat = @"EEEE aaahh:mm";
}
}
}else {
formatter.dateFormat = @"EEEE HH:mm";
}
// return [formatter stringFromDate:create];
return [NSString stringWithFormat:@"%@ %@",preDateStr,[formatter stringFromDate:create]];
}
} else { // 非今年
return preDateStr;
}
}
一个日期是否在7天之内,是否是今天,昨天,前天
NSDate 扩展
//NSDate分类
@implementation NSDate (Extension)
//是否是七天内
- (BOOL)isWithinSevendays {
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
fmt.dateFormat = @"yyyy-MM-dd";
NSDate *nowDate = [fmt dateFromString:[fmt stringFromDate:[NSDate date]]];
NSDate *selfDate = [fmt dateFromString:[fmt stringFromDate:self]];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *cmps = [calendar components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:selfDate toDate:nowDate options:0];
return cmps.year == 0
&& cmps.month == 0
&& cmps.day < 7;
}
//是否为今年
- (BOOL)isThisYear
{
// 日历
NSCalendar *calendar = [NSCalendar currentCalendar];
NSInteger nowYear = [calendar component:NSCalendarUnitYear fromDate:[NSDate date]];
NSInteger selfYear = [calendar component:NSCalendarUnitYear fromDate:self];
return nowYear == selfYear;
}
//判断是不是本星期
- (BOOL)isThisWeak{
// 日历
NSCalendar *calendar = [NSCalendar currentCalendar];
int unit = NSCalendarUnitWeekday | NSCalendarUnitMonth | NSCalendarUnitYear | kCFCalendarUnitDay | kCFCalendarUnitHour | kCFCalendarUnitMinute ;
//1.获得当前时间的 年月日
NSDateComponents *nowCmps = [calendar components:unit fromDate:[NSDate date]];
NSDateComponents *sourceCmps = [calendar components:unit fromDate:self];
// 对比时间差
NSDateComponents *dateCom = [calendar components:unit fromDate:[NSDate date] toDate:self options:0];
NSInteger subDay = labs(dateCom.day);
NSInteger subMonth = labs(dateCom.month);
NSInteger subYear = labs(dateCom.year);
if (subYear == 0 && subMonth == 0) { //当相关的差值等于零的时候说明在一个年、月、日的时间范围内,不是按照零点到零点的时间算的
if (subDay > 6) { //相差天数大于6肯定不在一周内
return NO;
} else { //相差的天数大于或等于后面的时间所对应的weekday则不在一周内
if (dateCom.day >= 0 && dateCom.hour >=0 && dateCom.minute >= 0) { //比较的时间大于当前时间
//西方一周的开始是从周日开始算的,周日是1,周一是2,而我们是从周一开始算新的一周
NSInteger chinaWeekday = sourceCmps.weekday == 1 ? 7 : sourceCmps.weekday - 1;
if (subDay >= chinaWeekday) {
return NO;
} else {
return YES;
}
} else {
NSInteger chinaWeekday = sourceCmps.weekday == 1 ? 7 : nowCmps.weekday - 1;
if (subDay >= chinaWeekday) { //比较的时间比当前时间小,已经过去的时间
return NO;
} else {
return YES;
}
}
}
} else { //时间范围差值超过了一年或一个月的时间范围肯定就不在一个周内了
return NO;
}
}
//是否是今天
- (BOOL)isToday
{
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
fmt.dateFormat = @"yyyy-MM-dd";
NSString *nowString = [fmt stringFromDate:[NSDate date]];
NSString *selfString = [fmt stringFromDate:self];
return [nowString isEqualToString:selfString];
}
//是否是昨天
- (BOOL)isYesterday
{
// 日期格式化类
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
fmt.dateFormat = @"yyyy-MM-dd";
NSDate *nowDate = [fmt dateFromString:[fmt stringFromDate:[NSDate date]]];
NSDate *selfDate = [fmt dateFromString:[fmt stringFromDate:self]];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *cmps = [calendar components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:selfDate toDate:nowDate options:0];
return cmps.year == 0
&& cmps.month == 0
&& cmps.day == 1;
}
//是否是前天
- (BOOL)iSThebeforeday {
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
fmt.dateFormat = @"yyyy-MM-dd";
NSDate *nowDate = [fmt dateFromString:[fmt stringFromDate:[NSDate date]]];
NSDate *selfDate = [fmt dateFromString:[fmt stringFromDate:self]];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *cmps = [calendar components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:selfDate toDate:nowDate options:0];
return cmps.year == 0
&& cmps.month == 0
&& cmps.day == 2;
}
//附
//当前时间区分上午/下午
- (NSDateComponents *)currentTime {
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDate* dt = [NSDate date];
unsigned unitFlags = NSCalendarUnitYear |
NSCalendarUnitMonth | NSCalendarUnitDay |
NSCalendarUnitHour | NSCalendarUnitMinute |
NSCalendarUnitSecond | NSCalendarUnitWeekday;
NSDateComponents* comp = [gregorian components: unitFlags
fromDate:dt];
return comp;
}
//是否凌晨比较使用
- (NSDateComponents *)dateComponets
{
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
unsigned unitFlags = NSCalendarUnitYear |
NSCalendarUnitMonth | NSCalendarUnitDay |
NSCalendarUnitHour | NSCalendarUnitMinute |
NSCalendarUnitSecond | NSCalendarUnitWeekday;
NSDateComponents* comp = [gregorian components: unitFlags
fromDate:self];
return comp;
}
@end
网友评论