• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Getting the NSDateFormatter strings from the user's preferences
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Getting the NSDateFormatter strings from the user's preferences


  • Subject: Re: Getting the NSDateFormatter strings from the user's preferences
  • From: "Sean McBride" <email@hidden>
  • Date: Fri, 20 May 2005 12:47:23 -0400
  • Organization: Rogue Research

On 5/19/05 6:08 PM, James Bucanek said:

>>So for example NSShortDateFormatString and NSDateFormatString aren't what
>>you want?  Could you elaborate on what your looking for?
>
>As I said, I was looking for a format string, or a formatter, that would
>format dates according to the user customizable date and time setting the
>International Preference Pane.

That's what NSShortDateFormatString and NSDateFormatString do.

>It turns out that the date and time format strings in the NSUserDefaults
>are NOT these values.  You can customize your date and time formatting
>preferences all day, and the strings in NSUserDefaults never change (and,
>they're not even very pretty to begin with).

Works for me. You can even try it in my app if you want (www.birthday-
buzzer.com), works with Hebrew, Japanese, etc.

Maybe you are caching the date string? There is an undocumented
notification you can receive if the user changes date formats:

	// Register for notification for when the user changes the date format
	// in System Preferences.
	//
	// This is not documented!  Thus risks breaking.
	//
	NSDistributedNotificationCenter*	ddnc = [NSDistributedNotificationCenter
defaultCenter];
	[ddnc addObserver:self
		selector:@selector(handleDefaultsChange:)
		name:@"AppleDatePreferencesChangedNotification"
		object:nil];

- (void)handleDefaultsChange:(id)sender
{
	// Check if the date format string actually did change...
	NSString*	oldDFS = [self currentDateFormatString];
	NSString*	newDFS = [[NSUserDefaults standardUserDefaults] stringForKey:
NSDateFormatString];
	if (![oldDFS isEqualToString:newDFS]) {
		// Remember the new format string
		[self setCurrentDateFormatString:newDFS];
	}
}

>Also, there are only two: NSShortDateFormatString and NSDateFormatString.
> The date formatting preferences in the International preference pane
>define four formats: Short, Medium, Long, and Full.  These correspond to
>the CFDateFormatter styles CFDateFormatterShortStyle,
>CFDateFormatterMediumStyle, CFDateFormatterLongStyle, and
>CFDateFormatterFullStyle.

Agreed.  Even before that, they correspond to the 'DateForm' type in
Classic Mac OS.  Pity Cocoa only has them now.  You might use an API like
LongDateString(), but I really can't recommend it as its not Unicode-savvy.

>It's obvious that support for these four styles has been added to the
>NSDateFormatter class in 10.4, but I needed a pre-10.4 solution.

To get abbrevDate I do the following:

	NSString* sdf = [[NSUserDefaults standardUserDefaults] stringForKey:
NSDateFormatString];

	// Make a mutable copy and change A to a and B to b
	// This gives abbreviated names for months and days
	NSMutableString*	sdfCopy = [sdf mutableCopy];
	NSRange wholeRange = NSMakeRange (0, [sdfCopy length]);
	(void)[sdfCopy replaceOccurrencesOfString:@"A" withString:@"a" options:0
range:wholeRange];
	(void)[sdfCopy replaceOccurrencesOfString:@"B" withString:@"b" options:0
range:wholeRange];

But your CFDateFormatter is certainly better.  It works in 10.3, yes?

--
____________________________________________________________
Sean McBride, B. Eng                 email@hidden
Rogue Research                        www.rogue-research.com
Mac Software Developer              Montréal, Québec, Canada


 _______________________________________________
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

  • Follow-Ups:
    • Re: Getting the NSDateFormatter strings from the user's preferences
      • From: James Bucanek <email@hidden>
References: 
 >Re: Getting the NSDateFormatter strings from the user's preferences (From: "Sean McBride" <email@hidden>)
 >Re: Getting the NSDateFormatter strings from the user's preferences (From: James Bucanek <email@hidden>)

  • Prev by Date: Re: Obtain directory size.
  • Next by Date: Re: Obtain directory size.
  • Previous by thread: Re: Getting the NSDateFormatter strings from the user's preferences
  • Next by thread: Re: Getting the NSDateFormatter strings from the user's preferences
  • Index(es):
    • Date
    • Thread