iOSNSDate的相關操作

來源:互聯網
上載者:User

標籤:

//擷取目前時間 時間根據格林威治時間顯示

    //時間 8小時 英國格林威治   7小時

    NSDate *date = [NSDate date];

    NSLog(@"%@",date);

    //NSZone 計算時區

    //根據目前時間 獲得一個時間 參數是秒

    NSDate *someDate = [NSDate dateWithTimeIntervalSinceNow:3600];

    NSLog(@"%@",someDate);

    

    //獲得很遠時間

    NSDate *futureDate = [NSDate distantFuture];

    NSLog(@"%@",futureDate);

    //獲得過去的很久的時間

    NSDate *pastDate= [NSDate distantPast];

    NSLog(@"%@",pastDate);

   //將來的200s

    NSDate *date1 = [NSDate dateWithTimeIntervalSinceNow:200];

    //比較date 和date1

    //返回更早的時間

    NSDate *dateEarly =[date earlierDate:date1];

    NSLog(@"date早的%@",dateEarly);

    //返回更晚的時間

    NSDate *dateLater = [date laterDate:date1];

//時間格式轉換類 NSDateFormatter

    //建立時間轉換對象

    NSDateFormatter * dateFormatter = [[NSDateFormatter alloc]init];

    //設定轉換的時間的格式

    //使用系統提供給我們的時間轉換格式

  //  dateFormatter.dateStyle =kCFDateFormatterFullStyle;

    //根據提供的時間 返回出來 時間對應字串

  // NSString *string = [dateFormatter stringFromDate:date];

   // NSLog(@"。。。 %@",string);

    

    //自定製的格式

    // year month day   hour minute second

    dateFormatter.dateFormat = @"yyyy:MM:dd HH:mm:ss";

    NSString *stringN = [dateFormatter stringFromDate:date];

    NSLog(@"%@",stringN);

    //根據輸入的字串返回時間

    NSString *str = @"2014-05-12 12:30:00";

    

    //根據輸入時間的格式 自定製出來 格式轉換格式

    NSDateFormatter *format = [[NSDateFormatter alloc]init];

    format.dateFormat = @"YYYY-MM-dd HH:mm:ss";

    //根據字串 輸出時間NSDate

    NSDate *someD = [format dateFromString:str];

    NSLog(@"%@",someD);

    //NSZone NSCalendarComponent

    

    

    //NSValue封裝 結構體 c語言數組

    int a[4] ={1,2,3,4};

    //建立一個NSValue

    NSValue *value = [[NSValue alloc]initWithBytes:a objCType:@encode(int[4])];

    

    

    int b[10];

    //取出來

    [value getValue:b];

    NSLog(@"%d",b[2]);

    

    //結構體

    struct student{

        int age;

    };

    

    //變數c  年齡 10

    struct student c;

    c.age=10;

    

    //將結構體 封裝到nsValue

    NSValue *value1 = [NSValue value:&c withObjCType:@encode(struct student)];

    

    

    //取學生出來

    struct student d;

    [value1 getValue:&d];

    

    NSLog(@"%d",d.age);

iOSNSDate的相關操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.