日期处理
2016-03-19 本文已影响43人
HeavenWong
效果图
效果图
- 还有显示年份
具体代码
写两个分类
- 分类一: NSCalendar
#import <Foundation/Foundation.h>
@interface NSCalendar (WMExtension)
+ (instancetype)calendar;
@end
#import "NSCalendar+WMExtension.h"
@implementation NSCalendar (WMExtension)
+ (instancetype)calendar {
if ([NSCalendar respondsToSelector:@selector(calendarWithIdentifier:)]) {
return [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
}else {
return [NSCalendar currentCalendar];
}
}
@end
二 . 分类二: NSDate
#import <Foundation/Foundation.h>
@interface NSDate (WMExtension)
/**
* 是否为今年
*/
- (BOOL)isThisYear;
/**
* 是否为今天
*/
- (BOOL)isToday;
/**
* 是否为昨天
*/
- (BOOL)isYesterday;
/**
* 是否为明天
*/
- (BOOL)isTomorrow;
@end
#import "NSDate+WMExtension.h"
#import "NSCalendar+WMExtension.h"
@implementation NSDate (WMExtension)
// 今年
- (BOOL)isThisYear {
// 判断self这个日期是否为今年
NSCalendar *calendar = [NSCalendar calendar];
// 年
NSInteger selfYear = [calendar component:NSCalendarUnitYear fromDate:self];
NSInteger nowYear = [calendar component:NSCalendarUnitYear fromDate:[NSDate date]];
return selfYear == nowYear;
}
// 今天
- (BOOL)isToday
{
// 判断self这个日期是否为今天
NSCalendar *calendar = [NSCalendar calendar];
// 获得年月日元素
NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
NSDateComponents *selfCmps = [calendar components:unit fromDate:self];
NSDateComponents *nowCmps = [calendar components:unit fromDate:[NSDate date]];
return selfCmps.year == nowCmps.year
&& selfCmps.month == nowCmps.month
&& selfCmps.day == nowCmps.day;
}
// 昨天
- (BOOL)isYesterday
{
// 判断self这个日期是否为昨天
// self == 2015-10-31 23:07:08 -> 2015-10-31 00:00:00
// now == 2015-11-01 14:39:20 -> 2015-11-01 00:00:00
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
fmt.dateFormat = @"yyyyMMdd";
NSString *selfString = [fmt stringFromDate:self]; // 20151031
NSString *nowString = [fmt stringFromDate:[NSDate date]]; // 20151101
NSDate *selfDate = [fmt dateFromString:selfString]; // 2015-10-31 00:00:00
NSDate *nowDate = [fmt dateFromString:nowString]; // 2015-11-01 00:00:00
NSCalendar *calendar = [NSCalendar calendar];
NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
NSDateComponents *cmps = [calendar components:unit fromDate:selfDate toDate:nowDate options:0];
return cmps.year == 0
&& cmps.month == 0
&& cmps.day == 1;
}
// 明天
- (BOOL)isTomorrow
{
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
fmt.dateFormat = @"yyyyMMdd";
NSString *selfString = [fmt stringFromDate:self]; // 20151031
NSString *nowString = [fmt stringFromDate:[NSDate date]]; // 20151101
NSDate *selfDate = [fmt dateFromString:selfString]; // 2015-10-31 00:00:00
NSDate *nowDate = [fmt dateFromString:nowString]; // 2015-11-01 00:00:00
NSCalendar *calendar = [NSCalendar calendar];
NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
NSDateComponents *cmps = [calendar components:unit fromDate:selfDate toDate:nowDate options:0];
return cmps.year == 0
&& cmps.month == 0
&& cmps.day == -1;
}
@end
在模型里面使用(根据具体需求来写)
- 模型WMTopic.m文件
#import "WMTopic.h"
#import "NSCalendar+WMExtension.h"
#import "NSDate+WMExtension.h"
@implementation WMTopic
static NSDateFormatter *fmt_;
static NSCalendar *calendar_;
/**
* 在第一次使用这个类的时候调用1次
*/
+ (void)initialize {
fmt_ = [[NSDateFormatter alloc] init];
calendar_ = [NSCalendar calendar];
}
- (NSString *)created_at // 表示时间的成员属性的get方法
{
// 获得发帖日期
fmt_.dateFormat = @"yyyy-MM-dd HH:mm:ss";
NSDate *createdAtDate = [fmt_ dateFromString:_created_at];
if (createdAtDate.isThisYear) { // 今年
if (createdAtDate.isToday) { // 今天
// 手机当前时间
NSDate *nowDate = [NSDate date];
NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
NSDateComponents *cmps = [calendar_ components:unit fromDate:createdAtDate toDate:nowDate options:0];
if (cmps.hour >= 1) { // 时间间隔 >= 1小时
return [NSString stringWithFormat:@"%zd小时前", cmps.hour];
} else if (cmps.minute >= 1) { // 1小时 > 时间间隔 >= 1分钟
return [NSString stringWithFormat:@"%zd分钟前", cmps.minute];
} else { // 1分钟 > 分钟
return @"刚刚";
}
} else if (createdAtDate.isYesterday) { // 昨天
fmt_.dateFormat = @"昨天 HH:mm:ss";
return [fmt_ stringFromDate:createdAtDate];
} else { // 其他
fmt_.dateFormat = @"MM-dd HH:mm:ss";
return [fmt_ stringFromDate:createdAtDate];
}
} else { // 非今年
return _created_at;
}
}
@end