Formatting Date to dd-MMM in iOS

爱⌒轻易说出口 提交于 2019-12-06 08:12:13
Henri Normak

Following from Duncan C's answer.

If the date is represented as a string, first turn it into a date and then use the NSDateFormatter on it. Assuming the format will remain the same it would be

[dateFormatter setDateFormat: @"yyyy-MM-dd HH:mm:ss Z"]; 
NSString *dateString = [dictionaryObject objectForKey: @"eventDate"];
NSDate *date = [dateFormatter dateFromString: dateString];

Then you can do what you are doing at the moment to the date object.

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MMM"];
NSString *formattedDateString = [dateFormatter stringFromDate:myDate];
[dateFormatter release];

It sounds like the object you are fetching from your dictionary with the key eventDate is a string, not a date.

Try this code:

NSDate *myDate=[tmpDict objectForKey:@"eventDate"]; 
NSLog(@"myDate class = %@", [myDate class]);

I bet it shows a class of NSString or one of it's subclasses, not an NSDate.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!