Re: NSDate, NSDateFormatter, and NSDatePicker
Re: NSDate, NSDateFormatter, and NSDatePicker
- Subject: Re: NSDate, NSDateFormatter, and NSDatePicker
- From: Matt Neuburg <email@hidden>
- Date: Fri, 13 Jan 2006 08:51:02 -0800
- Thread-topic: NSDate, NSDateFormatter, and NSDatePicker
On Fri, 13 Jan 2006 07:21:02 -0700, Pete Bates <email@hidden> said:
>I'm trying to set the display of a date picker to a date retrieved
>from a database in the form "mm-dd-yyyy". Eliminating the data
>retrieval code, the code looks like so:
>
>NSString * theDateString;
>theDateString=@"05-01-1994";
>
>NSDate * aDate;
>aDate=[[NSDate alloc]init];
>
>NSDateFormatter * aDateFormatter;
>aDateFormatter=[[NSDateFormatter alloc] init];
>[aDateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
>[aDateFormatter setDateFormat:@"mm-dd-yyyy"];
>
>aDate=[aDateFormatter dateFromString:theDateString];
>[dobPicker setDateValue:aDate];
>
>NSLog(@"The date format is: %@",[aDateFormatter dateFormat]);
>NSLog(@"The date string is: %@",theDateString);
>NSLog(@"The created date is: %@",aDate);
>
>
>But here is the Log Output:
>
>The date format is: mm-dd-yyyy
>The date string is: 05-01-1994
>The created date is: 1994-01-01 00:05:00 -0700
>
>and of course the date picker displays 1/1/1994
Simply do this:
NSString * theDateString=@"05-01-1994";
NSDate * aDate;
aDate = [NSCalendarDate dateWithString:theDateString
calendarFormat:@"%m-%d-%Y"];
If you really must use the whole overhead of an NSDateFormatter, then RTFM.
The format string you want is MM-dd-yyyy, not mm-dd-yyyy. M is month; m is
minute. Actually, M-d-y will do just fine; the extra letters are
meaningless.
You are also leaking both the NSDateFormatter and an unused NSDate object,
by the way. m.
--
matt neuburg, phd = email@hidden, <http://www.tidbits.com/matt/>
A fool + a tool + an autorelease pool = cool!
AppleScript: the Definitive Guide
<http://www.amazon.com/exec/obidos/ASIN/0596005571/somethingsbymatt>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden