时间操作相关的主要类有:
NSDate
NSDatFormatter
NSCalendar
NSDateComponents
另外介绍一个定时器对象NSTimer
用于描述一个日期时间,提供了一些基本的时间获取和比较方法
+ (instancetype)date //当前日期时间
+ (instancetype)dateWithTimeIntervalSinceNow:(NSTimeInterval)seconds
- (NSComparisonResult)compare:(NSDate *)anotherDate
- (NSTimeInterval)timeIntervalSinceDate:(NSDate *)anotherDate
用于字符串格式化NSDate对象
@property(copy) NSString *dateFormat //格式
- (NSString *)stringFromDate:(NSDate *)date
- (NSDate *)dateFromString:(NSString *)string
日历对象,在日历对象之上,进行NSDate与NSDateComponents的转换
+ (NSCalendar *)currentCalendar
- (NSDateComponents *)components:(NSCalendarUnit)unitFlags fromDate:(NSDate*)date
- (NSDate *)dateFromComponents:(NSDateComponents *)comps
用于对象的方式描述NSDate对象
@property NSInteger year
@property NSInteger month
@property NSInteger day
@property NSInteger hour
@property NSInteger minute
@property NSInteger second
...
NSTimer用于启动一个定时,定时执行指定的操作
指定的操作用SEL(方法)对象指定
//直接启动定时器
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)seconds target:(id)target selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)repeats
//创建一个不直接启动的定时器
+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)seconds target:(id)target selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)repeats
定时器的其他控制方法:
- (void)fire //启动
- (void)invalidate //使失效