iOS學習筆記25—NSDate的常用用法(一)

來源:互聯網
上載者:User

 

1. 建立或初始化可用以下方法

    用於建立NSDate執行個體的類方法有

    + (id)date;

    返回目前時間

 

    + (id)dateWithTimeIntervalSinceNow:(NSTimeInterval)secs;  

    返回以目前時間為基準,然後過了secs秒的時間

 

    + (id)dateWithTimeIntervalSinceReferenceDate:(NSTimeInterval)secs;

    返回以2001/01/01 GMT為基準,然後過了secs秒的時間

 

    + (id)dateWithTimeIntervalSince1970:(NSTimeInterval)secs;

    返回以1970/01/01 GMT為基準,然後過了secs秒的時間

 

    + (id)distantFuture;

    返回很多年以後的未來的某一天。

    比如你需要一個比現在(Now)晚(大)很長時間的時間值,則可以調用該方法。測試返回了4000/12/31 16:00:00

 

    + (id)distantPast;

    返回很多年以前的某一天。

    比如你需要一個比現在(Now)早(小)大很長時間的時間值,則可以調用該方法。測試返回了公元前0001/12/31 17:00:00

 

    用於建立NSDate執行個體的執行個體方法有

    - (id)addTimeInterval:(NSTimeInterval)secs;

    返回以目前的執行個體中儲存的時間為基準,然後過了secs秒的時間

 

    用於初始化NSDate執行個體的執行個體方法有

    - (id)init;

    初始化為目前時間。類似date方法

 

    - (id)initWithTimeIntervalSinceReferenceDate:(NSTimeInterval)secs;

    初始化為以2001/01/01 GMT為基準,然後過了secs秒的時間。類似dateWithTimeIntervalSinceReferenceDate:方法

 

    - (id)initWithTimeInterval:(NSTimeInterval)secs sinceDate:(NSDate *)refDate;

    初始化為以refDate為基準,然後過了secs秒的時間

 

    - (id)initWithTimeIntervalSinceNow:(NSTimeInterval)secs;

    初始化為以目前時間為基準,然後過了secs秒的時間

 

 

2. 日期之間比較可用以下方法

    - (BOOL)isEqualToDate:(NSDate *)otherDate;

    與otherDate比較,相同返回YES

 

    - (NSDate *)earlierDate:(NSDate *)anotherDate;

    與anotherDate比較,返回較早的那個日期

 

    - (NSDate *)laterDate:(NSDate *)anotherDate;

    與anotherDate比較,返回較晚的那個日期

 

    - (NSComparisonResult)compare:(NSDate *)other;

    該方法用於排序時調用:

      . 當執行個體儲存的日期值與anotherDate相同時返回NSOrderedSame

      . 當執行個體儲存的日期值晚於anotherDate時返回NSOrderedDescending

      . 當執行個體儲存的日期值早於anotherDate時返回NSOrderedAscending

 

 

3. 取回時間間隔可用以下方法

    - (NSTimeInterval)timeIntervalSinceDate:(NSDate *)refDate;

    以refDate為基準時間,返回執行個體儲存的時間與refDate的時間間隔

 

    - (NSTimeInterval)timeIntervalSinceNow;

    以目前時間(Now)為基準時間,返回執行個體儲存的時間與目前時間(Now)的時間間隔

 

    - (NSTimeInterval)timeIntervalSince1970;

    以1970/01/01 GMT為基準時間,返回執行個體儲存的時間與1970/01/01 GMT的時間間隔

 

    - (NSTimeInterval)timeIntervalSinceReferenceDate;

    以2001/01/01 GMT為基準時間,返回執行個體儲存的時間與2001/01/01 GMT的時間間隔

 

 

    + (NSTimeInterval)timeIntervalSinceReferenceDate;

    以2001/01/01 GMT為基準時間,返回目前時間(Now)與2001/01/01 GMT的時間間隔

 

 

4. 將時間表示成字串

    - (NSString *)description;

    以YYYY-MM-DD HH:MM:SS ±HHMM的格式表示時間。

    其中 "±HHMM" 表示與GMT的存在多少小時多少分鐘的時區差異。比如,若時區設定在北京,則 "±HHMM" 顯示為 "+0800"

 


 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.