2013-04-29 41 views
0

輸入:Fri, 26 Apr 2013 21:56:31 EDTEDT爲GMT轉換

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

[df1 setDateFormat:@"EE, dd MMM YYYY HH:mm:ss zzz"]; 

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

[df setDateFormat:@"yyyy-MM-dd HH:mm:ss ZZZ"]; 

NSString *startDateStr2 = [df1 stringFromDate:[NSDate date]]; 

NSLog(@"currentDate: %@", startDateStr2); 

但我得到輸出:Sat, 05 Jan 2013 07:26:31 GMT+05:30

回答

2

使用以下方法,該方法是用於改變NSDate的格式..

-(NSString *)changeDateFormat:(NSString*)stringDate dateFormat:(NSString*)dateFormat getwithFormat:(NSString *)getwithFormat{ 


    NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; 
    [dateFormatter setDateFormat:dateFormat]; 

    NSDate *date = [dateFormatter dateFromString:stringDate]; 

    dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; 
    [dateFormatter setDateFormat:getwithFormat]; 

    NSString *convertedString = [dateFormatter stringFromDate:date]; 
    NSLog(@"Converted String : %@",convertedString); 
    return convertedString; 
} 

以上方法調用其3個參數..

1. stringDate:傳遞你的日期,在字符串類型。

2. dateFormat:在傳入的字符串日期中使用的設置格式。例如,如果您的字符串日期是"14-03-2013 11:22 am",則將格式設置爲@"dd-MM-yyyy hh:mm a"

3 getwithFormat:你想要的,因爲如果你想約會14/03/2013設置格式只需要設置格式@"dd/MM/yyyy"

如何調用該見下面的例子與方法:

NSString *strSDate = [self changeDateFormat:@"14-03-2013 11:22 am" dateFormat:@"dd-MM-yyyy hh:mm a" getwithFormat:@"dd/MM/yyyy"]; 
NSLog(@"\n\n Now Date => %@",strSDate); 

輸出是:現在日期=> 14/03/2013

希望它有助於你...