时间操作相关的主要类有:
NSDate
NSDatFormatter
NSCalendar
NSDateComponents
另外介绍一个定时器对象NSTimer
NSDate
用于描述一个日期时间,提供了一些基本的时间获取和比较方法
+ (instancetype)date //当前日期时间
+ (instancetype)dateWithTimeIntervalSinceNow:(NSTimeInterval)seconds
- (NSComparisonResult)compare:(NSDate *)anotherDate
- (NSTimeInterval)timeIntervalSinceDate:(NSDate *)anotherDate
NSDateFormatter
用于字符串格式化NSDate对象
@property(copy) NSString *dateFormat //格式
- (NSString *)stringFromDate:(NSDate *)date
- (NSDate *)dateFromString:(NSString *)string
NSCalendar
日历对象,在日历对象之上,进行NSDate与NSDateComponents的转换
+ (NSCalendar *)currentCalendar
- (NSDateComponents *)components:(NSCalendarUnit)unitFlags fromDate:(NSDate*)date
- (NSDate *)dateFromComponents:(NSDateComponents *)comps
NSDateComponents
用于对象的方式描述NSDate对象
@property NSInteger year
@property NSInteger month
@property NSInteger day
@property NSInteger hour
@property NSInteger minute
@property NSInteger second
...
NSTimer
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 //使失效
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。