NSDate
1:[NSDatedate]得到的时间是格林威治时间。
2:- (id) dateByAddingTimeInterval : (NSTimeInterval) ti NS_AVAILABLE(10_6,2_0);返回一个增加ti秒数后的时间。
3:- (NSTimeInterval)timeIntervalSinceDate:(NSDate *)anotherDate;返回两个时间的秒数差
如:NSTimeInterval inter = [date1timeIntervalSinceDate:date2];如果date1比date2早则返回一个负值。
例如如下代码:
//2014-11-02 22:11:49.449 YYY[796:303] 2014-11-02 14:11:49 +0000
NSDate *date1 = [NSDatedate];NSLog(@"%@",date1);
//看上面的代码段你会发现,输出的并不是当前北京时间。
//2014-11-02 22:11:49.449 YYY[796:303] 2014-11-02 14:28:29 +0000
date1 = [date1 dateByAddingTimeInterval:1000];NSLog(@"%@",date1);
NSDate *date2 = [NSDatedate];NSTimeInterval inter = [date2 timeIntervalSinceDate:date1];
//也许你会以为输出的是-1000,其实基本上不会,特别是打断点的情况下,那就取决于date1 = [date1 dateByAddingTimeInterval:1000];这段代码执行时间与NSDate *date2 = [NSDate date];这段代码执行时间之间的差距了,即使没有断点,输出也不是-1000,例如: -999.999462等。
NSLog(@"%f",inter);
4:- (NSTimeInterval) timeIntervalSinceReferenceDate; 返回GMT时间2001年1月1日与对象时间之间的秒差。
//self.myDate是在另一个地方初始化的
NSTimeInterval inter2 = [self.myDatetimeIntervalSinceReferenceDate];
//返回GMT时间2001年1月1日与对象时间之间的秒差。
NSLog(@"%f",inter2);
5:- (NSComparisonResult)compare:(NSDate *)other;比较两个时间点。如果两个参数中有一个为空,结果都是0。正常情况是结果为0表示相等的意思,如果结果为-1则表示升序的意思。
NSComparisonResult compareresult = [self.myDatecompare:date1];
NSLog(@"%li",compareresult);
/*
//当self.myDate为空的时候,输出的是0。
2014-11-02 22:29:56.749 YYY[999:303] (null),2014-11-02 14:46:36 +0000
2014-11-02 22:29:58.874 YYY[999:303] 0
*/
NSComparisonResult compareresult2 = [date1compare:self.myDate];
NSLog(@"%li",compareresult2);
/*
2014-11-02 22:32:59.999 YYY[1060:303] 0//当self.myDate为空的时候,输出的是0。
*/