OC學習之道:關於NSDate和NSDateFormatter類的使用

//
//  main.m

//  Copyright (c) 2015年 tongxing. All rights reserved.
//
int main(int argc, const char * argv[])
{

    @autoreleasepool {

#pragma mark----NSDate的使用
    //1.獲取當前時間
        NSDate *newDate =[NSDate date];
        NSLog(@"%@",newDate);
    //2.計算從當前時間開始向後多長時間(明天的此時此刻)
        NSDate *dataLate = [NSDate dateWithTimeIntervalSinceNow:24*60*60];//默認零時區
        NSLog(@"%@",dataLate);
       //
        NSDate *data1 = [NSDate dateWithTimeIntervalSinceReferenceDate:8*60*60];
        NSLog(@"%@",data1);
    //3.1970年的參考日期點
        NSTimeInterval dataNumber = [newDate timeIntervalSince1970];
        NSLog(@"%f",dataNumber);
    //4.1990年的參考日期點
        NSTimeInterval dataNumber1 = [newDate timeIntervalSinceReferenceDate];
        NSLog(@"%f",dataNumber1);//很多時候利用這個方法來獲取一個時間戳,當我們在程序中看見類似的10位數字時候,可以聯想是否可能是時間戳?
    //5.日期之間的相互比較
        NSDate *newDate1 = [NSDate date];
        NSTimeInterval seconds = [newDate1 timeIntervalSinceDate:newDate];
        NSLog(@"%f",seconds);
        
       
#pragma mark----NSDateFormatter的使用   
    //NSDateFormatter這個類是用來處理日期與字符串之間轉換的方法。在很多時候,我們需要某個確定的時間點,但是NSDate僅僅提供瞭如何通過幾個特殊時間點,對於比較一般化的時間點,需要我們去計算時間段(TimeInterval),然後再獲取我們需要的時間點。這裏計算的過程相對較爲耗時,這樣我們提供了一個可以手寫一個字符串,然後將字符串直接轉化爲日期的類,方便快捷。
        
    //1.將NSdate轉變爲表示時間的NSString
        NSDateFormatter *dataFor = [[NSDateFormatter alloc]init];
        [dataFor setDateFormat:@"YYYY年MM月dd日HH時mm分ss秒"];
        //把日期轉化成字符串形式輸出,按照給定的日期格式,必須要通過日期對象來實現
        NSDate *date11 = [dataFor dateFromString:@"2015年04月08日10時50分55秒"];//這裏把字符串轉化爲日期對象
        NSLog(@"%@",date11);//打印出來的日期是格林尼治時間
        NSString *dateStr = [dataFor stringFromDate:date11];
        NSLog(@"%@",dateStr);//打印出來自定義的時間格式
       
    //2.表示時間的NSString轉換爲對應的NSdate對象
        NSDateFormatter * formatter1 = [[NSDateFormatter alloc]init];
        NSString *str = @"2013-12-10 10:30:00";
        [formatter1 setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
        NSDate * date2 = [formatter1 dateFromString:str];
        NSLog(@"%@",date2);
        
        
    //3.關於NSDateFormate的一些常用的方法:{知識擴展}
        
        
        NSDate *now= [NSDate date];
        NSDateFormatter *fmt = [[NSDateFormatter alloc]init];
        fmt.dateStyle = kCFDateFormatterMediumStyle;//日期格式
        fmt.timeStyle = kCFDateFormatterMediumStyle;//時間格式
        fmt.locale = [[NSLocale alloc]initWithLocaleIdentifier:@"en_US"];//zh_CN,則爲簡體中文輸出
        NSString *dataString =[fmt stringFromDate:now];
        NSLog(@"%@",dataString);
        
    //上面的實例中使用的參數是 kCFDateFormatterShortStyle,關於這個參數根據官方API下面給出具體參考:
       //@"en_US"
        typedef CF_ENUM(CFIndex, CFDateFormatterStyle) {    // date and time format styles
            kCFDateFormatterNoStyle = 0,       // 無輸出
            kCFDateFormatterShortStyle = 1,    // 10/29/12, 2:27 PM
            kCFDateFormatterMediumStyle = 2,   // Oct 29, 2012, 2:36:59 PM
            kCFDateFormatterLongStyle = 3,     // October 29, 2012, 2:38:46 PM GMT+08:00
            kCFDateFormatterFullStyle = 4      // Monday, October 29, 2012, 2:39:56 PM China Standard Time
        };
        
  /*   
        //@"zh_CH"
        typedef CF_ENUM(CFIndex, CFDateFormatterStyle) {    // date and time format styles
            kCFDateFormatterNoStyle = 0,       // 無輸出
            kCFDateFormatterShortStyle = 1,    // 12-10-29 下午2:52
            kCFDateFormatterMediumStyle = 2,   // 2012-10-29 下午2:51:43
            kCFDateFormatterLongStyle = 3,     // 2012年10月29日 GMT+0800下午2時51分08秒
            kCFDateFormatterFullStyle = 4      // 2012年10月29日星期一 中國標準時間下午2時46分49秒
        };
    */
        
        
        
        //4.也可以使用自定義的格式
         NSDate* now1 = [NSDate date];
         NSDateFormatter* fmt1 = [[NSDateFormatter alloc] init];
         fmt1.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
         fmt1.dateFormat = @"yyyy-MM-dd'T'HH:mm:ss";
         NSString* dateString1 = [fmt1 stringFromDate:now1];
         NSLog(@"%@", dateString1);
    }
return 0;
} 


發佈了31 篇原創文章 · 獲贊 4 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章