NSDate

NSDate * date = [[NSData alloc] init];
//NSData 時(shí)間類 主要用于時(shí)間數(shù)據(jù)的管理 存儲(chǔ)默認(rèn)的時(shí)間是格林 時(shí)間 因此使用時(shí)需要 主要時(shí)區(qū)的轉(zhuǎn)換
//1.獲取當(dāng)前系統(tǒng)時(shí)間
NSDate * dateNow = [[NSDate alloc] init];
NSDate * dateNow1 = [NSDate date];

    //2.自定義打印時(shí)間格式
    //創(chuàng)建時(shí)間格式對(duì)象
    NSDateFormatter * df = [[NSDateFormatter alloc] init];
    //選擇系統(tǒng)提供的格式
    //[df setTimeStyle:<#(NSDateFormatterStyle)#>]
    
    //自定義設(shè)置格式
    [df setDateFormat:@"G? YYYY-MM-dd EEE hh:mm:ss.SS"];
    //將時(shí)間對(duì)象dateNow 以df定義的格式 轉(zhuǎn)換為字符串
    //ps:表示月的時(shí)候 月必須用'M' 表示分'm' 秒's' 毫秒'S' 其余皆可
    NSString * dateStr = [df stringFromDate:dateNow];
    NSDate * dateNew = [df dateFromString:@"2015-04-25 10:55:22.00"];
  
    NSLog(@"%@",dateStr);
    //3.常用方法
    //date 與當(dāng)前系統(tǒng)時(shí)間的差
    //[date timeIntervalSinceNow];
    //date與1970的差
  //  [date timeIntervalSince1970];
    //date與dateNew的差
    //[date timeIntervalSinceDate:dateNew];
    
    //NSTimeInterval 實(shí)際就是double類型 主要用于表示時(shí)間的差值 相差的秒數(shù) 用%lf 打印
   // NSTimeInterval time = [date timeIntervalSinceDate:dateNew];

//兩種方式
//第一種 使用dateFormatter 輸入字符串 根據(jù)格式 輸出時(shí)間
NSString * string = @"2015-03 20, 03:20:20";
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc]init];
dateFormatter.dateFormat = @"yyyy-MM dd, HH:mm:ss";
//格式寫好了 轉(zhuǎn)換成 NSDate
NSDate* date = [dateFormatter dateFromString:string];
NSLog(@"%@",date);

    //第二種方式
    NSDateComponents *components = [[NSDateComponents alloc]init];
    //day表示天
    components.day = 20;
    //month表示月份
    components.month =2;
    //year表示年
    components.year =2015;
    //hour表示時(shí)
    components.hour =12;
    //minute表示分
    components.minute = 23;
    //second表示秒
    components.second = 20;

//部件寫好之后 要合成NSDate 使用 日歷
    NSCalendar * calender = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDate * date2 = [calender dateFromComponents:components];
    NSLog(@"%@",date2);
    
    NSLocale * locale = [NSLocale currentLocale];
    NSString * time = [date2 descriptionWithLocale:locale];
    NSLog(@"%@",time);
    
 //NSTimeZone 倒換時(shí)區(qū)時(shí)候用的類

//創(chuàng)建 一個(gè)時(shí)間 默認(rèn)為當(dāng)前時(shí)間 獲取的是 格林威治時(shí)間
NSDate * currentDate = [NSDate date];
//獲取當(dāng)前系統(tǒng)環(huán)境下的
NSLocale locale = [NSLocale currentLocale];
//把當(dāng)前系統(tǒng)下獲取的值 轉(zhuǎn)換字符串來(lái)打
//如果直接locale調(diào)用description
NSString * time = [currentDate descriptionWithLocale:locale];
//想要獲取一個(gè)以當(dāng)前時(shí)間為基礎(chǔ)的時(shí)間 60
60
NSDate* futureTime = [NSDate dateWithTimeIntervalSinceNow:3600];
//獲取一個(gè)小時(shí)以前的時(shí)間
NSDate * pastTime = [NSDate dateWithTimeIntervalSinceNow:-3600];
//如果想獲取一個(gè)過(guò)去很久的時(shí)間 到開始紀(jì)元的時(shí)候
NSDate * longlongAgo = [NSDate distantPast];
NSDate * longlongFuture = [NSDate distantFuture];
//做一個(gè)手表的 跟定時(shí)器(NSTimer) 一起來(lái)使用的
//比較倆個(gè)日期的 誰(shuí)早 睡晚
NSDate * earlyDate = [currentDate earlierDate:futureTime];
NSDate * laterDate = [currentDate laterDate:futureTime];
//比較倆個(gè)時(shí)間是否相等 返回值是布爾類型
BOOL ret= [earlyDate isEqualToDate:laterDate];

 //創(chuàng)建  dateFormatter 規(guī)定轉(zhuǎn)換 格式的
    NSDateFormatter * dateFormatter = [[NSDateFormatter alloc]init];
    //系統(tǒng)自帶了一些格式
    dateFormatter.dateStyle =NSDateFormatterMediumStyle;
    NSString * string= [dateFormatter stringFromDate:date];
    NSLog(@"%@",string);
    //自定義格式
    dateFormatter.dateFormat =@"yyyy:MM:dd HH:mm:ss";
    NSString * string1 = [dateFormatter stringFromDate:date];
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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