2016-01-06 72 views
0
NSString* lastModified = @"26 Jun 2015 11:16"; 
    activityTime   = [dateObject getDateFromString:lastModified andFormat:@"dd MMM yyyy HH:mm" toFormat:@"dd MMM, yyyy"]; 

我用過的格式有什麼問題?我總是得到activityTime變量爲nil。iOS中的日期格式不清晰...

我已經檢查過蘋果文檔也...同樣的格式給我的用例。但它不工作。

需要一些關於此的見解!

在此先感謝...

+0

的格式看起來不錯我。 '''getDateFromString:andFormat:toFormat:'''來自iOS API。我不認識它。 – Mateusz

回答

0
NSString *str = @"26 Jun 2015 11:16"; /// here this is your date with format yyyy-MM-dd 

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init] ; // here we create NSDateFormatter object for change the Format of date.. 
[dateFormatter setDateFormat:@"dd MMM yyyy HH:mm"]; //// here set format of date which is in your output date (means above str with format) 

NSDate *date = [dateFormatter dateFromString: str]; // here you can fetch date from string with define format 

dateFormatter = [[NSDateFormatter alloc] init] ; 
[dateFormatter setDateFormat:@"dd MMM, yyyy"];// here set format which you want... 

NSString *convertedString = [dateFormatter stringFromDate:date]; //here convert date in NSString 
NSLog(@"Converted String : %@",convertedString); 
+0

所以我不能使用getDateFromString方法嗎? –

+0

您也可以在該方法中添加上面的代碼並傳遞兩個參數 – sandy