help with date formatter

  • 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 | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.
  • 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 | <cocoa-dev...>
    > 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 | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.
  • 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 | <cocoa-dev...>
    >> 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 | <cocoa-dev...>
    > 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 | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.
previous month october 2003 next month
MTWTFSS
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31    
Go to today