How to convert XML date format to Cocoa

I have a date format: 2009-08-10T16: 03: 03Z

which I want to convert to: @ "MMM dd, HH: mm a"

I am extracting the XML format in NSString. I tried using NSDateformatter: NSDateFormatter * df = [[NSDateFormatter alloc] init]; [df setDateFormat: @ "MMM dd, HH: mm a"];

any ideas?

+5
source share
2 answers
NSString *yourXMLDate = @"2009-08-10T16:03:03Z" 
NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
[inputFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"];
NSDate *inputDate = [inputFormatter dateFromString:yourXMLDate];
NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setDateFormat:@"MMM dd, HH:mm a"];
NSString *outputDate = [outputFormatter stringFromDate:inputDate];

Hope this helps!

+6
source
    NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
    [dateFormatter setDateFormat:@"E, d LLL yyyy HH:mm:ss Z"]; // Thu, 18 Jun 2010 04:48:09 -0700
    NSDate *date = [dateFormatter dateFromString:self.currentDate];

    [item setObject:date forKey:@"date"];

    [items addObject:[item copy]];

edit the following code here

[dateFormatter setDateFormat:@"E, d LLL yyyy HH:mm:ss Z"]; // Thu, 18 Jun 2010 04:48:09 -0700

in the required format.

+2
source

All Articles