0
votes

I have a date format coming from server as 2013-12-30T14:59:00.+0000 i want to convert this into the the format MMM dd,yyyy h:mm a

Im doing as below

NSDateFormatter *dateFormatter =[[NSDateFormatter alloc] init];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];
[dateFormatter setTimeZone:timeZone];
NSDate *dateFromString = [dateFormatter dateFromString:selectedDate];
if (dateFromString != nil) 
{
       [dateFormatter setDateFormat:@"MMM dd,yyyy h:mm a"];
       stringDateFromDate = [dateFormatter stringFromDate:dateFromString];
}

But the "dateFromString" coming as nil.

Can any one help here

1

1 Answers

0
votes

In my view it's too late to advise. But I decided to write.

See this example for you selectedDate string and do something like:

NSString *selectedDate = @"2013-12-30T14:59:00.+0000";

// I donno what "T" means, that's just replace it to an empty string
selectedDate = [selectedDate stringByReplacingOccurrencesOfString:@"T" withString:@" "];

// get string without .+0000
NSRange range = [selectedDate rangeOfString:@"."];
selectedDate = [selectedDate substringToIndex:range.location];

NSString *stringDateFromDate;

NSDateFormatter *dateFormatter =[[NSDateFormatter alloc] init];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];
[dateFormatter setTimeZone:timeZone];

// necessarily set this date format!
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *dateFromString = [dateFormatter dateFromString:selectedDate];

if (dateFromString != nil)
{
[dateFormatter setDateFormat:@"MMM dd,yyyy h:mm a"];
stringDateFromDate = [dateFormatter stringFromDate:dateFromString];
}

// That's all. Output your string.
NSLog(@"%@", stringDateFromDate);