總的說(shuō)來(lái)
時(shí)間與字符串相關(guān)的,可以使用NSDateFormatter
時(shí)間與 年丶月丶日丶時(shí)丶分丶秒丶周相關(guān)的,可以使用NSCalendar和NSDateComponents
時(shí)間與地區(qū)相關(guān)的,可以使用NSTimeZone
1,NSDateFormatter?
日期轉(zhuǎn)字符串?
- (NSString *)stringFromDate:(NSDate *)date;?
字符串轉(zhuǎn)日期
?- (nullable NSDate *)dateFromString:(NSString *)string;??
2,NSdate?
獲取格林威治時(shí)間
?+ (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轉(zhuǎn)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轉(zhuǎn)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;