第08天OC語言(14):NSCalendar

  • 不要等到明天,明天太遙遠,今天就行動。
須讀:看完該文章你能做什么?

NSCalendar的基本使用

學習前:你必須會什么?(在這里我已經(jīng)默認你具備C語言的基礎(chǔ)了)

適合所有人,不需要懂的什么

注:(小白直接上手)

一、本章筆記
一、NSCalendar  日歷類
    1.初始化
        @property (class, readonly, copy) NSCalendar *currentCalendar; // user's preferred calendar
    2.獲取當前時間的年月日 時分秒
        - (NSDateComponents *)components:(NSCalendarUnit)unitFlags fromDate:(NSDate *)date;
    3.比較兩個時間之間的差值,比較相差多少年 多少月 多少日 多少時 多少分 多少秒
        - (NSDateComponents *)components:(NSCalendarUnit)unitFlags fromDate:(NSDate *)startingDate toDate:(NSDate *)resultDate options:(NSCalendarOptions)opts;

二、code
main.m
#pragma mark 14-NSCalendar

#pragma mark - 代碼
#import <Foundation/Foundation.h>
#pragma mark 類

#pragma mark - main函數(shù)
int main(int argc, const char * argv[])
{
#pragma 1.獲取當前時間的年月日 時分秒
    // 獲取當前時間
    NSDate *now = [NSDate date];
    NSLog(@"now = %@",now);
    // 日歷
    NSCalendar *calendar1 = [NSCalendar currentCalendar];
    
    // 利用日歷類 從當前時間對象中獲取 年月日時分秒(單獨獲取出來)
    // 從一個時間里面獲取 他的組成成員(年月日 時分秒)
    // components : 參數(shù)的含義是 : 問你需要獲取什么?
    // 一般情況下 如果一個方法接收一個參數(shù),這個參數(shù)是一個枚舉, 那么可以通過|符號,連接多個枚舉值
    NSCalendarUnit type = NSCalendarUnitYear |
                            NSCalendarUnitMonth |
                            NSCalendarUnitDay |
                            NSCalendarUnitHour |
                            NSCalendarUnitMinute |
                            NSCalendarUnitSecond;
    NSDateComponents *cmps =  [calendar1 components:type fromDate:now];
    NSLog(@"year = %ld",cmps.year);
    NSLog(@"month = %ld",cmps.month);
    NSLog(@"day = %ld",cmps.day);
    NSLog(@"hour = %ld",cmps.hour);
    NSLog(@"minute = %ld",cmps.minute);
    NSLog(@"second = %ld",cmps.second);

    
#pragma 2.比較兩個時間之間的差值,比較相差多少年 多少月 多少日 多少時 多少分 多少秒
    // 過去的時間
    NSString *str = @"2017-07-21 08:12:16 +0000";
    NSDateFormatter *formatter1 = [[NSDateFormatter alloc]init];
    formatter1.dateFormat = @"yyyy-MM-dd HH:mm:ss Z";
    NSDate *date1 = [formatter1 dateFromString:str];
    // 當前的時間
    NSDate *now1 = [NSDate date];
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    // 2.獲取當前時區(qū) 和 指定時間的時間差
    NSInteger seconds =  [zone secondsFromGMTForDate:now];
    NSLog(@"seconds = %lu",seconds);
    now1 = [now1 dateByAddingTimeInterval:seconds];
    NSLog(@"date = %@",date1);
    NSLog(@"now1 = %@",now1);

    // 比較兩個時間
    NSCalendar *calendar2 = [NSCalendar currentCalendar];
    NSDateComponents *cmps1 = [calendar2 components:type fromDate:date1 toDate:now1 options:0];
    NSLog(@"%ld年%ld月%ld日%ld時%ld分%ld秒",cmps1.year,cmps1.month,cmps1.day,cmps1.hour,cmps1.minute,cmps1.second);
    
    return 0;
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

  • 不要等到明天,明天太遙遠,今天就行動。 須讀:看完該文章你能做什么? NSDictionary的基本使用 學習前:...
    liyuhong閱讀 249評論 0 0
  • 不要等到明天,明天太遙遠,今天就行動。 須讀:看完該文章你能做什么? OC常用的結(jié)構(gòu)體 學習前:你必須會什么?(在...
    liyuhong閱讀 242評論 0 0
  • 不要等到明天,明天太遙遠,今天就行動。 須讀:看完該文章你能做什么? NSNumber的基本使用 學習前:你必須會...
    liyuhong閱讀 240評論 0 0
  • 不要等到明天,明天太遙遠,今天就行動。 須讀:看完該文章你能做什么? NSFileManager的基本使用 學習前...
    liyuhong閱讀 259評論 0 0
  • 不要等到明天,明天太遙遠,今天就行動。 須讀:看完該文章你能做什么? NSDate的基本使用 學習前:你必須會什么...
    liyuhong閱讀 243評論 0 0

友情鏈接更多精彩內(nèi)容