IOS --- 日期時間格式 轉換

1、如何如何將一個字符串如“ 20110826134106”裝化爲任意的日期時間格式,下面列舉兩種類型:

   NSString* string =@"20110826134106";
    NSDateFormatter*inputFormatter = [[[NSDateFormatter alloc] init]autorelease];

    [inputFormattersetLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]autorelease]];
    [inputFormattersetDateFormat:@"yyyyMMddHHmmss"];
    NSDate*inputDate = [inputFormatter dateFromString:string];
    NSLog(@"date= %@", inputDate);
    
    NSDateFormatter*outputFormatter = [[[NSDateFormatter alloc] init]autorelease]; 
    [outputFormattersetLocale:[NSLocale currentLocale]];
    [outputFormattersetDateFormat:@"yyyy年MM月dd日 HH時mm分ss秒"];
    NSString*str = [outputFormatter stringFromDate:inputDate];
    NSLog(@"testDate:%@",str);
兩次打印的結果爲:
 
   date= 2011-08-26 05:41:06 +0000
 
   testDate:2011年08月26日13時41分06秒

2、iOS-NSDateFormatter 格式說明:
G: 公元時代,例如AD公元
    yy:年的後2位
    yyyy:完整年
    MM:月,顯示爲1-12
    MMM:月,顯示爲英文月份簡寫,如 Jan
    MMMM:月,顯示爲英文月份全稱,如 Janualy
    dd:日,2位數表示,如02
    d:日,1-2位顯示,如 2
    EEE:簡寫星期幾,如Sun
    EEEE:全寫星期幾,如Sunday
    aa:上下午,AM/PM
    H:時,24小時制,0-23
    K:時,12小時制,0-11
    m:分,1-2位
    mm:分,2位
    s:秒,1-2位
    ss:秒,2位
    S:毫秒

常用日期結構:
yyyy-MM-dd HH:mm:ss.SSS
yyyy-MM-dd HH:mm:ss
yyyy-MM-dd
MM dd yyyy

3:新浪微博接口中,取得日期字符串 的轉換

NSString*dateStr = @"Wed May 222:27:08+0800 2012";

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

   [formater setDateFormat:@"EEE MMM d HH:mm:sszzzz yyyy"];
    NSDate* date = [formaterdateFromString:dateStr];
    NSLog(@"%@",date);
   
    //NSDate* now = [NSDatenow];
    double inter = fabs([datetimeIntervalSinceNow]);
    if( inter <60)
       NSLog(@"1 mins ago!");
    else if(inter< 60*60)
       NSLog(@"1 hours ago!");
    else if(inter< 60*60*24)
       NSLog(@"1 days ago!");
       NSLog(@"interval is %f min", inter/60);

  上面代碼在真機上運行後,發現取得date爲NULL,模擬器正常顯示,上網搜索後發現需要設置local, 果然設置後,真機正常

   NSLocale* local =[[[NSLocale alloc]initWithLocaleIdentifier:@"en_US"] autorelease];

   [formatter setLocale: local];

4、自定義顯示的 星期 格式

   使用NSDateFormatter轉換日期時,得到的英文字母的星期幾只能是這樣,如Sun, Mon, etc.

   如果想得到大寫字母的星期幾,可以這樣:


    NSArray*weekdayAry = [NSArray arrayWithObjects:@"SUN", @"MON", @"TUE",@"WED", @"THU", @"FRI", @"SAT", nil];


   dateFormatter = [[NSDateFormatter alloc] init];


   [dateFormatter setDateFormat:NSLocalizedString(@"YYYY.MM.dd.eee",nil)];


    //此處更改顯示的大寫字母的星期幾
 
  [dateFormattersetShortWeekdaySymbols:weekdayAry];


   [dateFormatter setLocale:[[NSLocale alloc]initWithLocaleIdentifier:@"en_US"] ]];


    NString *str= [dateFormatter stringFromDate:[NSDate date]];

 

5計算距離某一天還有多少時間

 

NSDate* toDate   = [ [ NSDate alloc]initWithString:@"2012-9-29 0:0:00 +0600" ];  

NSDate*  startDate  = [ [ NSDatealloc] init ];  

NSCalendar* chineseClendar = [ [ NSCalendar alloc ]initWithCalendarIdentifier:NSGregorianCalendar ]; 

 

NSUInteger unitFlags =  

NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit |NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit; 

 

NSDateComponents *cps = [chineseClendar components:unitFlagsfromDate:startDate toDate:toDate  options:0];  

 

NSInteger diffHour = [cps hour];  

NSInteger diffMin    = [cpsminute];  

NSInteger diffSec   = [cps second]; 

NSInteger diffDay   = [cps day]; 

NSInteger diffMon  = [cps month]; 

NSInteger diffYear = [cps year];  

 

NSLog(  @" From Now to %@, diff: Years:%d  Months: %d, Days; %d, Hours: %d, Mins:%d,sec:%d",   

[toDate description], diffYear, diffMon, diffDay, diffHour,diffMin,diffSec );  

   
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章