NSDate有個類別,如下:
@interface NSDate (NSDateCreation)
一個方法是:- (instancetype)initWithTimeInterval:(NSTimeInterval)secsToBeAdded sinceDate:(NSDate *)date;
這個方法官方的description是:Returns an NSDate object initialized relative to another given date by a given number of seconds.
返回一個NSDate對象,這個對象是另一個NSDate對象加減一個以秒爲單位的數的結果。
舉個例子;
我要獲取一個現在時間加一天的NSDate對象。
NSDate *newdate = [[NSDate date] initWithTimeInterval:24 * 60 * 60 sinceDate:[NSDate date]];
這裏的newdate就是獲得現在時間加一天時間的NSDate對象。類似的方法還有:
- (instancetype)initWithTimeIntervalSinceNow:(NSTimeInterval)secs;
- (instancetype)initWithTimeIntervalSince1970:(NSTimeInterval)secs;
另外還有一點
我們在用時間的時候,要注意時區,我們知道在地圖上各個時區的時間是不一樣的,這個就需要我們再做一些換行工作了。
日拱一卒,積少成多。