时间相关类和主要方法NSDateFormatter,NSDate

2017-02-20  本文已影响348人  youngZhou

总的说来

时间与字符串相关的,可以使用NSDateFormatter

时间与 年丶月丶日丶时丶分丶秒丶周相关的,可以使用NSCalendar和NSDateComponents

时间与地区相关的,可以使用NSTimeZone

1,NSDateFormatter 

日期转字符串 

- (NSString *)stringFromDate:(NSDate *)date; 

字符串转日期

 - (nullable NSDate *)dateFromString:(NSString *)string;  

2,NSdate 

获取格林威治时间

 + (instancetype)date; 

获取本地的localDate方法:

NSTimeZone * zone = [NSTimeZone systemTimeZone];

NSDate * localDate = [[NSDate date]dateByAddingTimeInterval:zone.secondsFromGMT];

 3,NSCalendar 

主要属性

NSCalendar *currentCalendar;

NSCalendarIdentifier calendarIdentifier;

NSLocale *locale;NSTimeZone *timeZone;      ***NSTimeZone***

NSUInteger firstWeekday;

NSUInteger minimumDaysInFirstWeek;  

NSDateComponents转NSDate

 - (nullable NSDate *)dateFromComponents:(NSDateComponents *)comps; 

- (nullable NSDate *)dateByAddingComponents:(NSDateComponents *)comps toDate:(NSDate *)date options:(NSCalendarOptions)opts;

 - (nullable NSDate *)dateByAddingUnit:(NSCalendarUnit)unit value:(NSInteger)value toDate:(NSDate *)date options:(NSCalendarOptions)options 

- (nullable NSDate *)dateBySettingUnit:(NSCalendarUnit)unit value:(NSInteger)v ofDate:(NSDate *)date options:(NSCalendarOptions)opts 

- (nullable NSDate *)dateBySettingHour:(NSInteger)h minute:(NSInteger)m second:(NSInteger)s ofDate:(NSDate *)date options:(NSCalendarOptions)opts

 NSDate转NSDateComponents 

- (NSDateComponents *)components:(NSCalendarUnit)unitFlags fromDate:(NSDate *)date; 

- (NSDateComponents *)components:(NSCalendarUnit)unitFlags fromDate:(NSDate *)startingDate toDate:(NSDate *)resultDate options:(NSCalendarOptions)opts; 

- (NSInteger)component:(NSCalendarUnit)unit fromDate:(NSDate *)date NS_AVAILABLE; 

- (NSDateComponents *)componentsInTimeZone:(NSTimeZone *)timezone fromDate:(NSDate *)date  

 4,NSDateComponents

NSCalendar *calendar      ***NSCalendar***

NSTimeZone *timeZone      ***NSTimeZone***

NSInteger era;

NSInteger year;

NSInteger month;

NSInteger day;

NSInteger hour;

NSInteger minute;

NSInteger second;

NSInteger nanosecondNSInteger weekday;

NSInteger weekdayOrdinal;

NSInteger quarter

NSInteger weekOfMonth

NSInteger weekOfYear

NSInteger yearForWeekOfYear

(getter=isLeapMonth) BOOL leapMonth

(nullable, readonly, copy) NSDate *date      ***NSDate*** 

- (void)setValue:(NSInteger)value forComponent:(NSCalendarUnit)unit NS_AVAILABLE(10_9, 8_0);

- (NSInteger)valueForComponent:(NSCalendarUnit)unit NS_AVAILABLE(10_9, 8_0);  

5,NSTimeZone

NSString *name;NSData *data;

NSTimeZone *systemTimeZone;

NSTimeZone *defaultTimeZone;

NSTimeZone *localTimeZone;

NSArray*knownTimeZoneNames;

NSDictionary*abbreviationDictionary

NSInteger secondsFromGMT;

NSString *abbreviation;

- (NSInteger)secondsFromGMTForDate:(NSDate *)aDate;

- (nullable NSString *)abbreviationForDate:(NSDate *)aDate;

+ (nullable instancetype)timeZoneWithName:(NSString *)tzName;

+ (nullable instancetype)timeZoneWithName:(NSString *)tzName data:(nullable NSData *)aData;

- (nullable instancetype)initWithName:(NSString *)tzName;

- (nullable instancetype)initWithName:(NSString *)tzName data:(nullable NSData *)aData;

+ (instancetype)timeZoneForSecondsFromGMT:(NSInteger)seconds;

+ (nullable instancetype)timeZoneWithAbbreviation:(NSString *)abbreviation;

上一篇下一篇

猜你喜欢

热点阅读