Re: formatting NSDate
Re: formatting NSDate
- Subject: Re: formatting NSDate
- From: Emma Whan <email@hidden>
- Date: Tue, 18 Feb 2003 18:09:12 +1100
Another bug to look out for with NSDateFormatter is that if the user
uses dd/mm/yyyy date order and has "leading zero for month" turned off
in the system preferences, then when you apply this format to a text
field, the user will not be able to enter ANY date whatsoever into the
field if allowNaturalLanguage is No. Even if allowNaturalLanguage is
yes, the user won't be able to enter dates except in US order
(mm/dd/yyyy) and those dates will then be switched to the opposite
order when the user leaves the field - most annoying.
I first noticed this bug because I live in Australia and my date format
is dd/mm/yyyy with leading zeros off. The format string returned from
my system preferences by [NSUserDefaults standardUserDefaults]
objectForKey:NSShortDateFormatString] is @"%e/%1m/%Y". I tried to
apply that format to a text field and it was a disaster. So you need
to be aware of this bug if you are date formatting a field that allows
user entry. I work around the problem by manually stripping the "1"
from in front of the "m" in the format string. It completely resolves
the bug, but does mean the user has to put up with leading zeros before
months. This is the code I use:-
//format date
NSDateFormatter *dateFormatter;
NSMutableString *dateFormat =[[NSMutableString alloc]
initWithString:[[NSUserDefaults standardUserDefaults]
objectForKey:NSShortDateFormatString]];
//look for "1m" in formatstring and change it to "m" (to overcome a
Cocoa bug) if user has dates before months (dd/mm/yy) - don't strip the
1 if user has mm/dd/yy date order
NSRange rangeOf1m = [dateFormat rangeOfString: @"1m"];
if (rangeOf1m.length != 0 && rangeOf1m.location != 1)
{
[dateFormat replaceCharactersInRange:rangeOf1m withString:@"m"];
}
dateFormatter = [[NSDateFormatter alloc]
initWithDateFormat:dateFormat allowNaturalLanguage:YES];
[dateDataCell setFormatter: dateFormatter];
[dateFormatter release];
[dateFormat release];
On Tuesday, February 18, 2003, at 03:20 PM,
email@hidden wrote:
>
Date: Mon, 17 Feb 2003 23:18:33 -0500
>
From: Michael Tsai <email@hidden>
>
Subject: Re: formatting NSDate
>
To: email@hidden
>
>
On Monday, February 17, 2003, at 08:54 PM, Jonathan Jackel wrote:
>
>
>> Is it possible format dates in the user's preffered Short Date
>
>> format as set in the International preference panel? I can't seem
>
>> to find it in NSDate or NSDateFormatter.
>
>
> [[NSUserDefaults standardUserDefaults] NSShortTimeDateFormatString]
>
>
Unfortunately, NSShortTimeDateFormatString does not follow the settings
>
in the International preference pane. NSTimeFormatString and
>
NSShortDateFormatString do, except that NSTimeFormatString doesn't add
>
the AM/PM designation when necessary.
>
>
Here's the code I'm using to print time-date strings:
>
>
@implementation NSDate (MJT)
>
- (NSString *)mjtShortTimeDateString
>
{
>
NSString *format = [NSString stringWithFormat:@"%@ %@",
>
[defaults
>
stringForKey:NSShortDateFormatString],
>
[defaults stringForKey:NSTimeFormatString]];
>
>
// ensure AM/PM if 12-hour
>
if ( [format mjtContainsString:@"I"] &&
>
![format mjtContainsString:@"%p"] )
>
format = [format stringByAppendingString:@"%p"];
>
>
// doesn't localize if you pass nil
>
NSDictionary *locale = [defaults dictionaryRepresentation];
>
>
return [self descriptionWithCalendarFormat:format
>
timeZone:nil
>
locale:locale];
>
}
>
@end
_______________________________________________
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.