Question

whenever i use this code

NSString *theDateS = [name valueForKey:@"startDate"];
        NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
        [dateFormat setDateFormat:@"yyyy-MM-dd hh:mm:ss a"];
        NSDate *theDateFormatted = [dateFormat dateFromString: theDateS];

I got an error message "NSInvalidArgumentException', reason: '-[__NSDate length]: unrecognized selector sent to instance 0x9249290'"

I was trying to change format 2014-06-10 16:00:00 +0000 to format 2014-06-10 04:00:00 PM pls help :(

Was it helpful?

Solution 2

Your entire block of code can simply be:

NSDate *date = name[@"startDate"];

Since the error indicates that you are actually getting an NSDate from your name dictionary.

Keep in mind that if you simply use NSLog on the NSDate, it will be shown in UTC format.

If you want to convert the date to a specific string format for display to the user, then update your code as follows:

NSDate *date = name[@"startDate"];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd hh:mm:ss a"];
NSString *theDateFormatted = [dateFormat stringFromDate:date];

OTHER TIPS

The variable theDateS is not what you think it is:

NSString * theDateS = [name valueForKey:@"startDate"]; 

It is actually an NSDate, not an NSString. Obj-c can be weird like that.

If you need to format it into an NSString, that's a pretty well researched topic on Stack Overflow:

https://stackoverflow.com/search?q=NSDateFormatter

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top