@font-face { font-family: "Courier New"; }@font-face { font-family: "宋體"; }@font-face { font-family: "Cambria Math"; }@font-face { font-family: "@宋體"; }@font-face { font-family: "Calibri"; }@font-face { font-family: "Cambria"; }@font-face { font-family: "Heiti SC Light"; }@font-face { font-family: "@Heiti SC Light"; }p.MsoNormal, li.MsoNormal, div.MsoNormal { margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 12pt; font-family: Cambria; }h4 { margin: 14pt 0cm 14.5pt; text-align: justify; line-height: 156%; page-break-after: avoid; font-size: 14pt; font-family: Calibri; }.MsoChpDefault { font-family: Cambria; }div.WordSection1 { page: WordSection1; }
NSDateFormatter類,是oc提供好的格式化時間的類,可以將我們獲取的時間,格式化成一個我們自己想要展示的方式。
NSDateFormatter會自動判斷我們格式化出來的時間,所在時區,比如我們讓他輸出年月日時分秒,這里會自動判斷我們所在的東八區,並獲取東八區的時間。
| // NSDateFormatter //定格式 //自動判斷時區,並補上時區 NSDateFormatter *format = [[NSDateFormatter alloc]init]; [format setDateFormat:@"yyyy年MM月dd日 HH時mm分ss秒"]; NSString *time = [format stringFromDate:date11]; NSLog(@"%@",time);//2015-04-21 11:07:07.541 OCLesson7_NSDate[1175:46103] 2015年04月21日 19時07分07秒 |
這里的格式化輸出為:yyyy表示年,MM表示月,dd表示天,HH表示時,mm表示分,ss表示秒。oc中只會識別這幾個格式,對年月日什么的都都不識別,所以,這幾個字符串放在哪個地方,就會在哪個地方輸出相應的年月日。不能改變這些關鍵字,否則不能識別。
dateFromString把字符串轉換成日期
首先,必須用NSDateFormatter的setDateFormat方法來設置格式化輸出的格式。才能在定義的字符串按照這個格式定義。
| NSDateFormatter *format = [[NSDateFormatter alloc]init]; [format setDateFormat:@"yyyy年MM月dd日 HH時mm分ss秒"]; NSString *time = [format stringFromDate:date11]; NSLog(@"%@",time);//2015-04-21 11:07:07.541 OCLesson7_NSDate[1175:46103] 2015年04月21日 19時07分07秒
//字符串轉換成NSDate NSString *str = @"2016年6月18日 08時59分01秒";//要與上面的@"yyyy年MM月dd日 HH時mm分ss秒"一一匹配,包括空格什么的。如果不匹配返回null NSDate *d = [[format dateFromString:str] dateByAddingTimeInterval:60*60*8]; NSLog(@"%@",d ); |
