[oc學習日記]NSDate,oc學習日記nsdate

來源:互聯網
上載者:User

[oc學習日記]NSDate,oc學習日記nsdate

首先介紹一下返回日期對象的方法

 1         //擷取當前日期時間 2         NSDate *now = [NSDate date]; 3         NSLog(@"%@",now); 4         //返回自從1970年之後經過多少秒(可正可負)之後的日期時間 5         NSDate *mydate1970 = [NSDate dateWithTimeIntervalSince1970:60*60]; 6         NSLog(@"%@",mydate1970); 7         //返回自從目前時間經過多少秒(可正可負)之後的日期時間 8         NSDate *mydatenow = [NSDate dateWithTimeIntervalSinceNow:-60*60]; 9         NSLog(@"%@",mydatenow);10         //返回給定時間經過多少秒(可正可負)之後的日期時間11         NSDate *mydatedate = [now dateByAddingTimeInterval:60*60];12         NSLog(@"%@",mydatedate);13         //擷取兩個時間之中比較早的那一個14         NSDate *ear = [mydate1970 earlierDate:mydatenow];15         NSLog(@"%@",ear);16         //擷取兩個時間中比較晚的那一個17         NSDate *last = [mydate1970 laterDate:mydatenow];18         NSLog(@"%@",last);

接下來介紹一下返回秒數的方法

1         //返回目前時間自1970年之後經過的秒數2         NSTimeInterval mytime1970 = [[NSDate date]timeIntervalSince1970];3         NSLog(@"%f",mytime1970);4         //返回給定時間距離現在經過的秒數5         NSTimeInterval mytimenow = [mydate1970 timeIntervalSinceNow];6         NSLog(@"%f",mytimenow);7         //擷取兩個時間的時間差8         NSTimeInterval timedis = [mydate1970 timeIntervalSinceDate:mydatenow];9         NSLog(@"%f",timedis);

接下來講述兩個 重點用法

1.日期格式轉化為字串格式

字串格式按照格式化對象的設定進行輸出 yyyy為年  也可寫為yy之輸出後兩位  MM只能是月 如果唯寫一個m會省去前面的0 如果月份是兩位元系統會自動加上預設的那位

a  為上下午  24小時制小時用H 12小時制用h

系統會自動給你轉化時差

1         //日期時間格式化對象  格式化的同時將事件轉化為當前時區的時間2         NSDateFormatter *dateformatter = [NSDateFormatter new];3         [dateformatter setDateFormat:@"yyyy年MM月dd日 HH時mm分ss秒 a vvvv"];4         NSString *mystedate = [dateformatter stringFromDate:now];5         NSLog(@"%@",mystedate);

2.字串轉化為日期格式

注意:轉化出來的日期會有時差

1         //日期格式的字串轉化為日期對象  格式化的內容要與字串內容一樣2         NSString *mystrdate = @"2012-12-12 10:12:59";3         NSDateFormatter *dateformatter1 = [NSDateFormatter new];4         [dateformatter1 setDateFormat:@"yyyy-MM-dd HH:mm:ss"];5         NSDate *mystrdd = [dateformatter1 dateFromString:mystrdate];6         NSLog(@"%@",mystrdd);

 

 

 

接下來為大家介紹時區

首先是用的不太多隻需瞭解的內容

1         //系統時區2         NSTimeZone *syszone = [NSTimeZone systemTimeZone];3         //本地時區4         NSTimeZone *localzone = [NSTimeZone localTimeZone];5         //預設時區6         NSTimeZone *defaultzone = [NSTimeZone defaultTimeZone];7         NSLog(@"%@",syszone);8         NSLog(@"%@",[localzone abbreviation]);9         NSLog(@"%@",[defaultzone abbreviation]);

查看系統中收錄了的時區

1         //系統中所有時區的數組2         NSArray *zonearr = [NSTimeZone knownTimeZoneNames];3         NSLog(@"%@",zonearr);4         //系統中所有時區的字典5         NSDictionary *zonedic = [NSTimeZone abbreviationDictionary];6         NSLog(@"%@",zonedic);

最後就是重中之重——————時差轉換

1.格林尼治時區轉化

1         //轉換時差2         NSString *strdate = @"2015年6月6日 8時8分56秒";3         NSDateFormatter *formatter = [NSDateFormatter new];4         [formatter setDateFormat:@"yyyy年MM月dd日 HH時mm分ss秒"];5         //設定時區6         [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];7         NSDate *date = [formatter dateFromString:strdate];8         NSLog(@"%@",date);

2.設定時區

1          //轉換時差2          NSString *strdate = @"2015年6月6日 8時8分56秒";3          NSDateFormatter *formatter = [NSDateFormatter new];4          [formatter setDateFormat:@"yyyy年MM月dd日 HH時mm分ss秒"];5          //兩種方式都可以  最好用第一種 第二種有的時區會沒有收錄因而無法轉換6          [formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];7          [formatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];8          NSDate *date = [formatter dateFromString:strdate];9          NSLog(@"%@",date); 

3.格林尼治時間差轉換

 1         //計算系統時區和格林尼治的時間差  得到的是秒數 2         NSInteger timeoff = [syszone secondsFromGMT]; 3         NSLog(@"%zi",timeoff); 4         //轉換時差 5         NSString *strdate = @"2015年6月6日 8時8分56秒"; 6         NSDateFormatter *formatter = [NSDateFormatter new]; 7         [formatter setDateFormat:@"yyyy年MM月dd日 HH時mm分ss秒"]; 8         //在目前時間加上時差得到正確時間 9         NSDate *firdate = [formatter dateFromString:strdate];10         NSDate *date = [firdate dateByAddingTimeInterval:timeoff];11         NSLog(@"%@",date);

 

相關文章

聯繫我們

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