Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Thoroughly confused on date formatting



On Thu, 28 Sep 2006 09:24:10 +0100, Mike Abdullah <email@hidden>
said:
>In my app, I want to have an NSTextField for formatting times only.
>I see that NSDateFormatter (the 10.4 version) has the ability to do
>this using the setTimeStyle: method
>
>I need to display hours and minutes only.  I could just force the
>user to my chosen format, but I'd rather they have their own choice
>of 12/24 hour clock etc.
>
>I figured that the NSDateFormatterShortStyle constant would pull what
>I needed, but the docs say:
>
>"The format for these date and time styles is not exact because they
>depend on the locale, user preference settings, and the operating
>system version. Do not use these constants if you want an exact format."
>
>So, if I want to set a date formatter up to display hours and minutes
>only, where can I reliably pull this information from?  Will I have
>to take the user's preference from NSUserDefaults and manually parse
>it?

Here is what I do:

NSDateFormatter *dateFormatter =
   [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
NSMutableString* f =
   [NSMutableString stringWithString:[dateFormatter dateFormat]];

Aha, so now I have the user's preferences expressed as a mutable string in
ICU format. So now I mutate the string to combine the user's preferences
with my own (e.g. get rid of everything but hours and minutes, in your
case), and then:

[dateFormatter setDateFormat:f];

m.   

-- 
matt neuburg, phd = email@hidden, <http://www.tidbits.com/matt/>
A fool + a tool + an autorelease pool = cool!
AppleScript: the Definitive Guide - Second Edition!
<http://www.amazon.com/gp/product/0596102119>



 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/email@hidden

This email sent to email@hidden



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.