本文主要是介绍iOS 之NSDate的Category,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
iOS 之NSDate的Category
判断日期是是否是今年?今天?
NSDate+Extension.h
#import <Foundation/Foundation.h>
@interface NSDate (Extension)//比较from和self的时间差值
- (NSDateComponents *)deltaFrom:(NSDate *)from;//是否为今年
- (BOOL)isThisYear;//是否为今天
- (BOOL)isToday;//是否为昨天
- (BOOL)isYesterday;//是否为明天
- (BOOL)isTomorrow;@end
NSDate+Extension.m
#import "NSDate+Extension.h"@implementation NSDate (Extension)
- (NSDateComponents *)deltaFrom:(NSDate *)from
{// 日历NSCalendar *calendar = [NSCalendar currentCalendar];// 比较时间NSCalendarUnit unit = NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;return [calendar components:unit fromDate:from toDate:self options:0];
}- (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
{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
{// 2014-12-31 23:59:59 -> 2014-12-31// 2015-01-01 00:00:01 -> 2015-01-01// 日期格式化类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)isTomorrow{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:nowDate toDate:selfDate options:0];return cmps.year == 0&& cmps.month == 0&& cmps.day == 1;}
@end
简单使用TopicModel
#import "TopicModel.m"文件中
- (NSString *)create_time
{// 日期格式化类NSDateFormatter *fmt = [[NSDateFormatter alloc] init];// 设置日期格式(y:年,M:月,d:日,H:时,m:分,s:秒)fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";// 帖子的创建时间NSDate *create = [fmt dateFromString:_create_time];if (create.isThisYear) { // 今年if (create.isToday) { // 今天NSDateComponents *cmps = [[NSDate date] deltaFrom:create];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 (create.isYesterday) { // 昨天fmt.dateFormat = @"昨天 HH:mm:ss";return [fmt stringFromDate:create];} else if(creat.isTomorrow){fmt.dateFormat = @"明天 HH:mm:ss";return [fmt stringFromDate:create];}else { // 其他fmt.dateFormat = @"MM-dd HH:mm:ss";return [fmt stringFromDate:create];}} else { // 非今年return _create_time;}
}
这篇关于iOS 之NSDate的Category的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!