views:

98

answers:

2

Hi,

I store some dates in my NSMutableArray, I want to retrieve them and change the format. For the moment I do this:


NSDate *myDate = [self.dates objectAtIndex:0];
NSLog(@"myDate:%@", myDate); // 2010-03-02
NSDateFormatter *formatDate = [[NSDateFormatter alloc] init];
[formatDate setDateFormat:@"MMMM d YYYY"];

NSString *newDate = [formatDate stringFromDate:myDate];
NSLog(@"newDate: %@", newDate); // NULL

the result for newDate is NULL, and I want it to be something like that: "March 3, 2010"

thanks,

+3  A: 

That works for me, but I replaced the first line with:

NSDate *myDate = [NSDate date];

My output:

2010-03-02 19:29:08.045 app[11464:a0f] myDate:2010-03-02 19:29:08 -0800
2010-03-02 19:29:08.048 app[11464:a0f] newDate: March 2 2010

My best guess is that [self.dates objectAtIndex:0] isn't returning what you want it to.

Carl Norum
thats Not what I want, its too easy if I use the [NSDate date];I already store all my dates inside an array now I want to use them, my first line is good because I can read my date
ludo
@ludo, if your array contained a date, your code would work. That array contains something else. Your second line would work if it contained strings, for example.
Carl Norum
Actually, I would guess you have a string in there, not a date - that's the most likely case.
Carl Norum
in fact my Array contained some dates but with this format ---> 2010-03-02 not like the NSDate 2010-03-02 11:38:08 +0800
ludo
@ludo: NSDates do not have a format, so that is quite odd. That's what a date formatter is for. Try logging the class of `myDate` to be sure that it is in fact an NSDate and not accidentally something else that happens to have a date-like description.
Chuck
yes I have a string inside, but when I do NSDate *myDate = [self.dates objectAtIndex:0]; it didn't consider it like a date?
ludo
@ludo, my point is that it doesn't contain `NSDate` objects. If it did, your code would work. Try printing out `[myDate class]` to see what it really is.
Carl Norum
@ludo: No. A string is still a string even if you make an NSDate variable point to it. Giving the variable an `NSDate*` type is just lying to compiler about what the variable points to. See my answer here for more info on how variables work in Objective-C: http://stackoverflow.com/questions/1313836/cocoa-string-question/1313952#1313952
Chuck
@ludo - `NSString` is not the same as `NSDate`. Check out the `NSDate` documentation for how to convert back and forth.
Carl Norum
@ludo: there is no "conversion" done when you assign a NSString to a NSDate variable.
dreamlax
so no possible conversion, thats bad~~~ thanks anyway
ludo
@ludo, there are conversions, but not just by assigning a pointer of one type to a pointer of another type. That's just crazy.
Carl Norum
ok I understand, but its strange that there is no way for the NSDateFormater to recognize the elements of my string
ludo
@ludo, `NSDateFormatter` *can* do that. Go check out the documentation.
Carl Norum
+1  A: 

Since your dates array contains strings, you have to convert the string to an NSDate and then you can re-convert the date to the format you want.

NSDateFormatter *strToDateFmt = [[NSDateFormatter alloc] init];
[strToDateFmt setDateFormat:@"yyyy-MM-dd"];
NSDate *myDate = [strToDateFmt dateFromString:[self.dates objectAtIndex:0]];
[strToDateFmt release];
NSLog(@"myDate:%@", myDate);

NSDateFormatter *formatDate = [[NSDateFormatter alloc] init];
[formatDate setDateFormat:@"MMMM d YYYY"];
NSString *newDate = [formatDate stringFromDate:myDate];
[formatDate release];
NSLog(@"newDate: %@", newDate);
DyingCactus
I just to it by myself just after they told me to watch the documentation, stringFromDate: was my solution. Thank you, your code is the same and its working
ludo