I am doing an application where I am using the following link http://iosdevelopertips.com/cocoa/date-formatters-examples-take-3.html to convert the date from string.
I am using below code to same by following above tutorial link. when I NSlog PresentDateTime it gives 20120111 13:03:49.263+05:30.
so I am taking this format and want to convert into the format of “EEEE MMMM d, YYYY h:mm a, zzz“.
To do so when I assign the same to the variable
NSString *today = presentDateTime;
when I NSLog Datestring I am getting result NULL can I know where I am going wrong? please help me to solve this issue by suggesting what changes to be done
//code
-(NSString *)DateFormat:(NSString *) presentDateTime
{
presentDateTime = [presentDateTime stringByReplacingOccurrencesOfString:@"T" withString:@" "];
presentDateTime = [presentDateTime stringByReplacingOccurrencesOfString:@" +" withString:@"+"];
presentDateTime = [presentDateTime stringByReplacingOccurrencesOfString:@"-" withString:@""];
NSLog(@"Present date::::%@",presentDateTime);
NSString *today = presentDateTime;
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyyMMdd HH:mm:ss:SSSZZZ"];
NSDate *date = [dateFormat dateFromString:today];
[dateFormat setDateFormat:@"EEEE MMMM d, YYYY h:mm a, zzz"];
NSString *dateString = [dateFormat stringFromDate:date];
[dateFormat release];
NSLog(@"dateString:::Date>>>>:%@::::\n%@",dateString, presentDateTime);
return dateString;
Your date format pattern for parsing the date is wrong/incomplete. For the string
20120111 13:03:49.263+05:30the format would beYYYYMMdd HH:mm:ss.SSSZZZ. See this table for the format string patterns.Edit: After playing around with the code, it turns out that the
NSDateParserchokes on the timezone+05:30. It expects+0530, without the colon. Here’s how to remove it: