2016-01-06 38 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