iOS-Foundation框架(NSDate.h解读)

iOS开发之OC(Foundation框架--NSDate.h解读)


#import <Foundation/NSObject.h>


@class NSString;


NS_ASSUME_NONNULL_BEGIN


FOUNDATION_EXPORT NSString * const NSSystemClockDidChangeNotificationNS_AVAILABLE(10_6,4_0);


typedef double NSTimeInterval;


#define NSTimeIntervalSince1970  978307200.0


@interface NSDate :NSObject <NSCopying,NSSecureCoding>

//2001/01/0100:00:00 GMT为基准时间的实例保存的时间与2001/01/01 GMT的时间间隔

@property (readonly)NSTimeInterval timeIntervalSinceReferenceDate;

//初始化为当前时间。(GMT)

- (instancetype)initNS_DESIGNATED_INITIALIZER;

//初始化为2001/01/01 00:00:00 GMT为基准,然后过了ti秒的时间。(GMT)

- (instancetype)initWithTimeIntervalSinceReferenceDate:(NSTimeInterval)tiNS_DESIGNATED_INITIALIZER;

- (nullable instancetype)initWithCoder:(NSCoder *)aDecoderNS_DESIGNATED_INITIALIZER;


@end




@interface NSDate (NSExtendedDate)


//****************取回时间间隔可用以下方法*****************************

//anotherDate为基准时间,返回实例保存的时间与anotherDate的时间间隔

- (NSTimeInterval)timeIntervalSinceDate:(NSDate *)anotherDate;

//以当前时间(Now)为基准时间的实例保存的时间与当前时间(Now)的时间间隔

@property (readonly)NSTimeInterval timeIntervalSinceNow;

//以以1970/01/01 GMT为基准时间的实例保存的时间与以1970/01/01 GMT的时间间隔

@property (readonly)NSTimeInterval timeIntervalSince1970;

//弃用

- (id)addTimeInterval:(NSTimeInterval)secondsNS_DEPRECATED(10_0,10_6, 2_0, 4_0);

//返回以当前NSDate对象为基准,偏移多少秒后得到的新NSDate对象

- (instancetype)dateByAddingTimeInterval:(NSTimeInterval)tiNS_AVAILABLE(10_6,2_0);


//****************日期之间比较可用以下方法*****************************

//anotherDate比较,返回较早的那个日期

- (NSDate *)earlierDate:(NSDate *)anotherDate;

//anotherDate比较,返回较晚的那个日期

- (NSDate *)laterDate:(NSDate *)anotherDate;

/*

   该方法用于排序时调用:

   . 当实例保存的日期值与anotherDate相同时返回NSOrderedSame

   . 当实例保存的日期值晚于anotherDate时返回NSOrderedDescending

   . 当实例保存的日期值早于anotherDate时返回NSOrderedAscending

*/

- (NSComparisonResult)compare:(NSDate *)other;

//otherDate比较,相同返回YES

- (BOOL)isEqualToDate:(NSDate *)otherDate;

//YYYY-MM-DD HH:MM:SS ±HHMM的格式表示时间。(其中 “±HHMM”表示与GMT的存在多少小时多少分钟的时区差异。比如,若时区设置在北京,则 “±HHMM”显示为 “+0800″

@property (readonly,copy) NSString *description;

- (NSString *)descriptionWithLocale:(nullableid)locale;


//2001/01/0100:00:00 GMT为基准时间的当前(Now)时间与2001/01/01 GMT的时间间隔

+ (NSTimeInterval)timeIntervalSinceReferenceDate;


@end



@interface NSDate (NSDateCreation)

//****************创建或初始化可用以下方法****************

//[NSDate date]获取的是GMT时间,格林威治时间(GMT)和世界协调时间(UTC)处于同个时区

+ (instancetype)date;//返回当前时间(GMT)

//返回以当前时间为基准,然后过了secs秒的时间。(GMT)

+ (instancetype)dateWithTimeIntervalSinceNow:(NSTimeInterval)secs;

 //返回以2001/01/01 00:00:00 GMT为基准,然后过了secs秒的时间。(GMT)

+ (instancetype)dateWithTimeIntervalSinceReferenceDate:(NSTimeInterval)ti;

//返回以1970/01/01 00:00:00 GMT为基准,然后过了secs秒的时间。(GMT)

+ (instancetype)dateWithTimeIntervalSince1970:(NSTimeInterval)secs;

//返回以date GMT为基准,然后过了secsToBeAdded秒的时间。(GMT)

+ (instancetype)dateWithTimeInterval:(NSTimeInterval)secsToBeAdded sinceDate:(NSDate *)date;


//返回很多年以后的未来的某一天。(比如你需要一个比现在(Now)()很长时间的时间值,则可以调用该方法。测试返回了4001-01-01 00:00:00 +0000)。(GMT)

+ (NSDate *)distantFuture;

//返回很多年以前的某一天。(比如你需要一个比现在(Now)()大很长时间的时间值,则可以调用该方法。测试返回了0000-12-30 00:00:00 +0000)。(GMT)

+ (NSDate *)distantPast;


//初始化为以当前时间为基准,然后过了secs秒的时间。(GMT)

- (instancetype)initWithTimeIntervalSinceNow:(NSTimeInterval)secs;

//初始化为1970/01/01 00:00:00 GMT为基准,然后过了secs秒的时间。(GMT)

- (instancetype)initWithTimeIntervalSince1970:(NSTimeInterval)secs;

//初始化为date GMT为基准,然后过了secsToBeAdded秒的时间。(GMT)

- (instancetype)initWithTimeInterval:(NSTimeInterval)secsToBeAdded sinceDate:(NSDate *)date;


/*

    //获取的GMT时间,要想获得某个时区的时间,以下代码可以解决这个问题

    NSDate *date = [NSDate dateWithTimeIntervalSinceReferenceDate:10];

    NSTimeZone *zone = [NSTimeZone systemTimeZone];

    NSInteger interval = [zone secondsFromGMTForDate: date];

    NSDate *localeDate = [date  dateByAddingTimeInterval: interval];

    NSLog(@"%@",date);

    NSLog(@"%@", localeDate);

*/

@end


NS_ASSUME_NONNULL_END


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值