iOS:NSDate 扩展

iOS NSDate 扩展

NSDate+CF.h

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

typedef NS_ENUM(NSInteger, NSDateFormaterMode) {
   
    NSDateFormaterModeDefault,      /** 年-月-日 时:分:秒 */
    NSDateFormaterModeTime,         /** 年-月-日 时:分 */
    NSDateFormaterModeDay,          /** 年-月-日 */
    NSDateFormaterModeDayAnother,   /** *年*月*日 */
    NSDateFormaterModeTimeSecond,   /** 时:分:秒 */
    NSDateFormaterModeTimeMinute    /** 时:分 */
};

@interface NSDateFormatter (CF)

+ (NSDateFormatter *)dateFormaterWithMode:(NSDateFormaterMode)formaterMode;

@end

@interface NSDate (CF)

@property (nonatomic, assign) NSInteger year;               //年
@property (nonatomic, assign) NSInteger month;              //月
@property (nonatomic, assign) NSInteger day;                //日
@property (nonatomic, assign) NSInteger hour;               //时
@property (nonatomic, assign) NSInteger minute;             //分
@property (nonatomic, assign) NSInteger second;             //秒

@property (nonatomic, assign) NSInteger weekday;            //周几
@property (nonatomic, assign) NSInteger weekmonth;          //第几周(本月)
@property (nonatomic, assign) NSInteger weekyear;           //第几周(本年)
@property (nonatomic, copy)   NSString *weekStr;            //星期几

@property (nonatomic, copy)   NSString *yesterday;          //昨天
@property (nonatomic, copy)   NSString *tomorrow;           //明天
@property (nonatomic, copy)   NSString *dayAfterTomorrow;   //后天

//date -> 格式化date
+(NSDate *)dateToDate:(NSDate *)date formaterMode:(NSDateFormaterMode)formaterMode;

//字符串 -> date
+(NSDate *)stringToDate:(NSString *)string formaterMode:(NSDateFormaterMode)formaterMode;

//data -> 字符串   获取指定格式的日期字符串
+(NSString *)dateToString:(NSDate *)date formaterMode:(NSDateFormaterMode)formaterMode;

//字符串 -> 字符串   获取指定格式的日期字符串
+(NSString *)regularDateString:(NSString *)originalDate formaterMode:(NSDateFormaterMode)formaterMode;

//判断是否为今天
+ (BOOL)isTodayWithDate:(NSDate *)date;

//判断是否为昨天
+ (BOOL)isYesterdayWithDate:(NSDate *)date;

//判断是否为昨天、今天、明天、后天……
+ (NSInteger)dayWithDate:(NSDate *)date;

/*
 * 需要传入的时间格式 2017-06-14 14:18:54    暂不考虑跨年
 * 时间显示规则
 * 当天       时:分
 * 前一天      昨天
 * 其余       月:日
 */
+(NSString *)inputTimeStr:(NSString *)timeStr;

//计算时长 天 - 小时 - 分
+(NSString *)intervalSinceFromDate:(NSString *)fromDate toDate:(NSString *)toDate dateModel:(NSDateFormaterMode)dateModel;


//时间戳转日期
+(NSString *)timestampToDate:(NSString *)timestamp;

//日期对比
+ (int)compareOneDate:(NSDate *)date1 withAnotherDate:(NSDate *)date2;

//日期对比
+ (int)compareOneDateString:(NSString *)date1 withAnotherDateString:(NSString *)date2;

//获取农历日期
+(NSDictionary *)getChineseCalendarWithDate:(NSDate *)date;

+ (NSInteger)minuteStartTime:(NSString *)startTime endTime:(NSString *)endTime;

//计算时间差
+ (NSString *)dateInterval:(NSDate *)date time:(NSInteger)minute;

//跳转到偏差时间点
+(NSDate *)getResultTime:(NSString *)time second:(NSInteger)second;

//获取当前时间戳  (以秒为单位)
+(NSString *)getNowTimeTimestamp;

//获取当前时间戳  (以毫秒为单位)
+(NSString *)getNowTimeTimestamp3;

//将本地日期字符串转为UTC日期字符串
//本地日期格式:2013-08-03 12:53:51
//可自行指定输入输出格式
+(NSString *)getUTCFormateLocalDate:(NSString *)localDate;

//将UTC日期字符串转为本地时间字符串
//输入的UTC日期格式2013-08-03T04:53:51+0000
+(NSString *)getLocalDateFormateUTCDate:(NSString *)utcDate;

@end

NS_ASSUME_NONNULL_END

NSDate+CF.m

#import "NSDate+CF.h"

@implementation NSDateFormatter (CF)

+ (NSDateFormatter *)dateFormaterWithMode:(NSDateFormaterMode)formaterMode {
   
    NSString *stringFormat = @"";
    
    switch (formaterMode) {
   
            
        case NSDateFormaterModeDefault:
            stringFormat = @"yyyy-MM-dd HH:mm:ss";
            break;
            
        case NSDateFormaterModeTime:
            stringFormat = @"yyyy-MM-dd HH:mm";
            break;
        
        case NSDateFormaterModeDay:
            stringFormat = @"yyyy-MM-dd";
            break;
        
        case NSDateFormaterModeDayAnother:
            stringFormat = @"yyyy年MM月dd日";
            break;
            
        case NSDateFormaterModeTimeSecond:
            stringFormat = @"HH:mm:ss";
            break;
            
        case NSDateFormaterModeTimeMinute:
            stringFormat = @"HH:mm";
            break;
            
        default:
            stringFormat = @"yyyy-MM-dd HH:mm:ss";
            break;
    }
    
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:stringFormat];
    return dateFormat;
}

@end

@implementation NSDate (CF)

#pragma mark  - 年
-(NSInteger)year{
   
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:NSCalendarUnitYear fromDate:self];
    return [components year];
}

#pragma mark - 月
-(NSInteger)month{
   
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:NSCalendarUnitMonth fromDate:self];
    return [components month];
}

#pragma mark - 日
-(NSInteger)day{
   
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:NSCalendarUnitDay fromDate
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值