Re: Date Formatter year input without year display
Re: Date Formatter year input without year display
- Subject: Re: Date Formatter year input without year display
- From: Ashley Clark <email@hidden>
- Date: Wed, 10 Dec 2008 14:51:56 -0600
On Dec 10, 2008, at 10:48 AM, Paul Bruneau wrote:
Hi-
I ran into a problem with my production scheduling application as
the new year approached.
In IB I am using a 10.0 date formatter bound to NSDate properties of
a model object.
I used: %1m/, %1I:%M%p
which gave me something like: 12/23, 12:30PM
This was used mostly for display, but the user is also able to type
into the edit field in order to change the value.
As the new year came into play, he might have changed 12/23 to 1/5.
My thinking was that if he entered just 1/5 it would assume the
current year, which seems to be correct.
But I thought if he entered 1/5/2009, then Cocoa would help me out
and do the right thing.
In fact, even when entering the year explicitly, the formatter still
made the data be 1/5/2008.
I had to add the year to the formatter string to make it accept the
year as input.
Is this expected? Is there a good way to only display the month and
day, and yet allow the year to be entered if desired?
I've yet to find a better way but I accomplished something similar,
parsing multiple date formats with one formatter, by making a sort of
meta-formatter.
Basically define a subclass of NSDateFormatter that references a
display formatter and an array of input formatters. I used static
instances of the formatters but you could also just change the
formatter settings as well.
Then override all of the NSFormatter methods passing them on to either
the display formatter or iterating through your input formatters (most
specific to least specific) until one can successfully parse the input
string.
Ashley
_______________________________________________
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