Re: help with date formatter
Re: help with date formatter
- Subject: Re: help with date formatter
- From: Jonathan Jackel <email@hidden>
- Date: Sun, 12 Oct 2003 08:17:24 -0400
I have seen the same bug. Please report it. As described below, the
problem is with any A.M. time, whether typed or programmatically set.
My workaround was to subclass NSDateFormatter as follows:
@interface JJDateFormatter : NSDateFormatter {
}
@implementation JJDateFormatter
- (BOOL)getObjectValue:(id *)obj forString:(NSString *)string
errorDescription:(NSString **)error
/* Returns by reference in obj an NSCalendarDate representing the
date entered by the user.
NSDateFormatter does not properly handle times entered in natural
language format, even though [NSDate dateWithNaturalLanguageString]
does. NSDateFormatter converts all times to AM, even when the user
appends PM (with or without capitals, with or without periods).
This formatter will simply take the string entered into the
textfield and convert it into an NSCalendarDate using
dateWithNaturalLanguageString: and dateWithCalendarFormat:
The dateWithNaturalLanguageString: returns null if the string
cannot be converted into a date. In that case, the formatter returns
an empty string. Otherwise, the formatter performs no validation. */
{
NSCalendarDate *theDate = [[NSDate
dateWithNaturalLanguageString:string] dateWithCalendarFormat:[self
dateFormat] timeZone:nil];
if (theDate)
{
*obj = theDate;
} else {
*obj = @"";
if (error) *error = @"Not a Date";
}
return YES;
}
- (NSAttributedString *)attributedStringForObjectValue:(id)obj
withDefaultAttributes:(NSDictionary
*)attrs
{
return [[[NSAttributedString alloc]
initWithString:[obj description] attributes:attrs]
autorelease];
}
- (NSString *)stringForObjectValue:(id)anObject
{
return [anObject description];
}
On Saturday, October 11, 2003, at 10:42 PM, Theodore Petrosky wrote:
>
I have a textField with a date formatter. when I type
>
today into the field it works great however, when I
>
try to add a date programatically, I have a problem
>
with tabbing or hitting the return...
>
>
NSCalendarDate *newDate = [NSCalendarDate
>
dateWithString:@"Friday, July 1, 2001 23:45 PM"
>
calendarFormat:@"%A, %B %e, %Y %I:%M %p"];
>
NSLog(@"this is a string representation
>
%@",[newDate description]);
>
[dateField setObjectValue:newDate];
>
>
That populates the textField but if I tab from this
>
textfield to another (or type the return) the PM
>
changes to AM. I am running out of ideas.
>
>
Maybe I am loading the textField incorrectly.!
>
>
Please help,
>
>
Ted
>
>
__________________________________
>
Do you Yahoo!?
>
The New Yahoo! Shopping - with improved product search
>
http://shopping.yahoo.com
>
_______________________________________________
>
cocoa-dev mailing list | email@hidden
>
Help/Unsubscribe/Archives:
>
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
>
Do not post admin requests to the list. They will be ignored.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.