Re: NSDateFormatter strangeness
Re: NSDateFormatter strangeness
- Subject: Re: NSDateFormatter strangeness
- From: Greg Hoover <email@hidden>
- Date: Tue, 18 Nov 2008 09:06:35 -0800
Ok, I buy that, but then what's the problem here:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat: @"dd/MM/YYYY"];
NSString *str = @"3/10/2008";
NSLog(@"%@\r%@\r%@", str, [formatter dateFromString: str], [formatter
stringFromDate: [formatter dateFromString: str]]);
[formatter release];
This code uses the formatter both directions but prints out the
following:
3/10/2008
2007-12-23 00:00:00 -0800
23/12/2007
So it's clear that there is a problem converting from the string to
date. Is there an issue with creating static NSDateFormatters? Its
curious because some of the date formatters in my app work as
expected, others do not.
Greg
On Nov 18, 2008, at 12:06 AM, Nathan Kinsinger wrote:
On Nov 17, 2008, at 6:23 PM, Greg Hoover wrote:
I am using NSDateFormatter. The code snippet included shows this.
Greg
On Nov 17, 2008, at 3:38 PM, Kyle Sluder wrote:
I think it's a known issue. The documentation for -[NSDate
descriptionWithCalendarFormat:timeZone:locale:] (which I can only
assume that -[NSDate description] calls) has this caveat:
"There are several problems with the implementation of this method
that cannot be fixed for compatibility reasons. To format a date
correctly, you should consider using a date formatter object instead
(see NSDateFormatter and Data Formatting Programming Guide for
Cocoa)."
So, don't log NSDate objects, send them through an NSDateFormatter
instead.
--Kyle Sluder
The method -[NSDateFormatter dateFromString:] returns an NSDate,
which is what NSLog sees, which results in the problem that Kyle
refers to.
First you need to create an NSDate that NSDateFormatter can then
format into a string, like so:
NSLog(@"%@ -> %@", dateString, [dateFormatter stringFromDate:[NSDate
dateWithNaturalLanguageString:dateString]]);
Hope that helps,
--Nathan
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden