NSDate 是一个提供创建日期、比较日期、表示日期、计算时间间隔和其他相似功能的抽象类。
重要时间点:2001-01-01 00:00:00 GMT -- 系统参考日期 System‘s Reference Date
1970-01-01 00:00:00 GMT -- Unix时间起始点
1. 任务
1.1 创建并初始化日期对象
init()
返回一个初始化为当前日期和时间的NSDate对象。
convenience init(timeIntervalSinceNow seconds: NSTimeInterval)
返回一个初始化为与当前日期和时间相差指定秒数的NSDate对象,负秒数代表过去的一个时间。
convenience init(timeInterval seconds: NSTimeInterval, sinceDate refDate: NSDate!)
返回一个初始化为与指定日期和时间相差指定秒数的NSDate对象。
init(timeIntervalSinceReferenceDate seconds: NSTimeInterval)
返回一个初始化为与格林威治时间2001年1月1日0点0分0秒相差指定秒数的NSDate对象。
convenience init(timeIntervalSince1970 seconds: NSTimeInterval)
返回一个初始化为与格林威治时间1970年1月1日0点0分0秒相差指定秒数的NSDate对象。
1.2 获取时间边界
class func distantFuture() -> AnyObject!
创建并返回一个代表遥远未来(以世纪为单位)的某个日期的NSDate对象,当需要一个NSDate对象来本质上忽略日期参数时可传递此值。
class func distantPast() -> AnyObject!
创建并返回一个代表古老过去(以世纪为单位)的某个日期的NSDate对象,可以用作控制日期,一个确保的临时界限。
1.3 比较日期
func isEqualToDate(_ anotherDate: NSDate!) -> Bool
返回一个表明指定对象是否是NSDate对象且和receiver一样的布尔值。
func earlierDate(_ anotherDate: NSDate!) -> NSDate!
返回两者相比更早的那个NSDate对象。
func laterDate(_ anotherDate: NSDate!) -> NSDate!
返回两者相比更晚的那个NSDate对象。
func compare(_ anotherDate: NSDate!) -> NSComparisonResult
返回一个表明两者时间顺序的NSComparisonResult值,注意比较对象anotherDate不能为nil。
1.4 获取时间间隔
var timeIntervalSinceNow: NSTimeInterval { get }
返回接收者和当前日期时间的时间间隔,如果接收者早于当前时间,返回负值。
var timeIntervalSinceReferenceDate: NSTimeInterval { get }
返回接收者和格林威治时间2001年1月1日0点0分0秒的时间间隔,如果接收者早于该时间,返回负值。
var timeIntervalSince1970: NSTimeInterval { get }
返回接收者和格林威治时间1970年1月1日0点0分0秒的时间间隔,如果接收者早于该时间,返回负值。
class func timeIntervalSinceReferenceDate() -> NSTimeInterval
返回格林威治时间2001年1月1日0点0分0秒和当前日期时间的时间间隔。
func timeIntervalSinceDate(_ anotherDate: NSDate!) -> NSTimeInterval
返回两者的时间间隔,如果接收者早于anotherDate,返回负值。
1.5 增加一段时间
func dateByAddingTimeInterval(_ seconds: NSTimeInterval) -> Self!
返回一个与接收者相差指定秒数的新的NSDate对象。
1.6 以字符串形式表示日期
var description: String! { get }
返回接收者的字符串形式,仅可用来调试。
func descriptionWithLocale(_ locale: AnyObject!) -> String!
根据地区返回接收者的字符串形式,如果locale为nil,与description相同返回国际形式“2001-03-24 10:45:32 +0600”。
2. 常量
var NSTimeIntervalSince1970: CDouble { get }
NSDate提供了一个表示从格林威治时间1970年1月1日0点0分0秒到2001年1月1日0点0分0秒时间间隔的常量。
3. 通知
NSSystemClockDidChangeNotification
当系统时钟发生变化时发出通知。