.h
文件
//
// NSDate+XMGExtension.h
// 01-百思不得姐
//
// Created by xiaomage on 15/7/27.
// Copyright (c) 2015年 小码哥. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface NSDate (XMGExtension)
/**
* 比较from和self的时间差值
*/
- (NSDateComponents *)deltaFrom:(NSDate *)from;
/**
* 是否为今年
*/
- (BOOL)isThisYear;
/**
* 是否为今天
*/
- (BOOL)isToday;
/**
* 是否为昨天
*/
- (BOOL)isYesterday;
@end
.m
文件
//
// NSDate+XMGExtension.m
// 01-百思不得姐
//
// Created by xiaomage on 15/7/27.
// Copyright (c) 2015年 小码哥. All rights reserved.
//
#import "NSDate+XMGExtension.h"
@implementation NSDate (XMGExtension)
- (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
//{
// // 日历
// NSCalendar *calendar = [NSCalendar currentCalendar];
//
// NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
//
// 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)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;
}
@end
说明:出自MJ