Reputation: 1316
I thought I had the "hard" part done but that doesn't seem to be the case. I retrieve a date from an RSS feed that I would like to convert to a NSDate and then change it to the simple format (MM/DD/YYYY). The date comes in this format on the RSS feed:
Thu, 1 Nov 2012 17:41:56 CST
I have converted it to an NSDate successfully.
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"E, d MMM yyyy HH:mm:ss zzz"];
NSDate *date = [[NSDate alloc] init];
//[df setDateStyle:NSDateFormatterMediumStyle]; Using these seems to change what it expects from the string I guess?
//[df setTimeStyle:NSDateFormatterMediumStyle];
date = [df dateFromString:string];
[self.tempItem setPubDate:[df dateFromString:string]];
NSLog(@"%@", [df dateFromString:string]);
So I get my date just fine, but like I said before I want to just convert it to a simple date. When I use setDateStyle I end up with null values. When I don't use it, I get correct dates, just not in the format I want it to end up being.
Upvotes: 3
Views: 108
Reputation: 884
Just create another NSDateFormatter to convert NSDate to NSString
NSDateFormatter *dfSimple = [[NSDateFormatter alloc] init];
[dfSimple setTimeStyle:NSDateFormatterNoStyle];
[dfSimple setDateStyle:NSDateFormatterMediumStyle];
NSLog(@"%@", [dfSimple stringFromDate: date]);
It is better to create 2 NSDateFormatter objects in this case
Upvotes: 1
Reputation:
just not in the format I want it to end up being.
Because NSLog()
doesn't know about the format - how would it? NSDate
doesn't have any built-in "format". You have to obtain a string from the date object by using NSDateFormatter
once again. Basically the workflow you'll have to use is roughly:
NSDate
object from the date formatterNSDateFormatter
to the format you desireNSString
using the date and the date formatter objects.Upvotes: 2