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:46:42 -0400
Ted, I just read your post again and I may have been a bit quick on the
draw. This looks wrong to me:
NSCalendarDate *newDate = [NSCalendarDate
dateWithString:@"Friday, July 1, 2001 23:45 PM"
calendarFormat:@"%A, %B %e, %Y %I:%M %p"];
You've got a 12-hour time format, but you are assigning the time in a
quasi-24-hour format. Have you tried using 11:45 PM instead of 23:45
PM? Or using %H instead of %I, and dropping the PM and %p?
Jonathan
On Sunday, October 12, 2003, at 08:17 AM, Jonathan Jackel wrote:
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.
_______________________________________________
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.