一、日期
-
NSDate 日期类型
- 当前时间: [NSDate date]
-
时间比较,推荐转换成NSDate类型,然后在进行比较
-
1.[NSDate1 timeIntervalSinceDate: NSDate2]-> NSDate1 和 NSDate2相差多少秒
2.NSCalendar 日历对象
-
-
NSString 与 NSDate 相互转换
1.要利用NSDateFormatter,
注意
:转换的前提必须要告诉转换的日期格式NSDateFormatter,否则转换不成功为null2.
NSString -》 NSDate
-
3.NSDate - > NSString
NSCalendar 日历对象
-
必知:
- 1.如何获取NSDate的年月日时分秒
- 根据一个时间单元获取时间元素
比较两个时间的差值
- 1.两个日期日期比较前,要先设置日期格式NSDateFormatter,然后将两个比较的日期都转换成NSDate,再进行比较之间的差值
- 封装NSDate比较封装到分类中
- 注意:日历对象已经帮我们封装了,现在我们自己实现以下判断:是否是今天,是否是昨天,是否是明天等方法
#import <Foundation/Foundation.h>
@interface NSDate (JPExtension)
- (NSDateComponents *)intervalToDate:(NSDate *)date;
- (NSDateComponents *)intervalToNow;
/**
* 是否为今年
*/
- (BOOL)isThisYear;
/**
* 是否为今天
*/
- (BOOL)isToday;
/**
* 是否为昨天
*/
- (BOOL)isYesterday;
/**
* 是否为明天
*/
- (BOOL)isTomorrow;
@end
#import "NSDate+JPExtension.h"
@implementation NSDate (JPExtension)
- (NSDateComponents *)intervalToDate:(NSDate *)date
{
// 日历对象
NSCalendar *calendar = [NSCalendar currentCalendar];
// 想比较哪些元素
NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
// 比较
return [calendar components:unit fromDate:self toDate:date options:0];
}
- (NSDateComponents *)intervalToNow
{
return [self intervalToDate:[NSDate date]];
}
/**
* 是否为今年
*/
- (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)isToday
{
NSCalendar *calendar = [NSCalendar currentCalendar];
NSCalendarUnit unit = NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear;
NSDateComponents *nowCmps = [calendar components:unit fromDate:[NSDate date]];
NSDateComponents *selfCmps = [calendar components:unit fromDate:self];
return nowCmps.year == selfCmps.year
&& nowCmps.month == selfCmps.month
&& nowCmps.day == selfCmps.day;
}
/**
* 是否为昨天
*/
- (BOOL)isYesterday
{
// now : 2015-02-01 00:01:05 --> 2015-02-01
// self : 2015-01-31 23:59:10 --> 2015-01-31
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
fmt.dateFormat = @"yyyy-MM-dd";
// 获得只有年月日的时间
NSString *nowString = [fmt stringFromDate:[NSDate date]];
NSDate *nowDate = [fmt dateFromString:nowString];
NSString *selfString = [fmt stringFromDate:self];
NSDate *selfDate = [fmt dateFromString:selfString];
// 比较
NSCalendar *calendar = [NSCalendar currentCalendar];
NSCalendarUnit unit = NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear;
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 = @"yyyy-MM-dd";
// 获得只有年月日的时间
NSString *nowString = [fmt stringFromDate:[NSDate date]];
NSDate *nowDate = [fmt dateFromString:nowString];
NSString *selfString = [fmt stringFromDate:self];
NSDate *selfDate = [fmt dateFromString:selfString];
// 比较
NSCalendar *calendar = [NSCalendar currentCalendar];
NSCalendarUnit unit = NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear;
NSDateComponents *cmps = [calendar components:unit fromDate:selfDate toDate:nowDate options:0];
return cmps.year == 0
&& cmps.month == 0
&& cmps.day == -1;
}
@end
- 在发帖模型中 利用自定义的NSDate分类,对 发帖时间进行处理 后返回处理后的日期
#pragma mark - getter
- (NSString *)created_at
{
// 日期格式化类
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
// NSString -> NSDate
NSDate *createdAtDate = [fmt dateFromString:_created_at];
// 比较【发帖时间】和【手机当前时间】的差值
NSDateComponents *cmps = [createdAtDate intervalToNow];
if (createdAtDate.isThisYear) {
if (createdAtDate.isToday) { // 今天
if (cmps.hour >= 1) { // 时间差距 >= 1小时
return [NSString stringWithFormat:@"%zd小时前", cmps.hour];
} else if (cmps.minute >= 1) { // 1分钟 =< 时间差距 <= 59分钟
return [NSString stringWithFormat:@"%zd分钟前", cmps.minute];
} else {
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;
}
}