Right Way to Display Dates+Times
Right Way to Display Dates+Times
- Subject: Right Way to Display Dates+Times
- From: Will Price <email@hidden>
- Date: Sun, 16 Dec 2001 15:43:45 -0800
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
I have spent an inordinate amount of time trying to figure out how to
*correctly* display localized Dates/Times from Cocoa. Mind you, this
is one very simple call in Carbon, so I'm not feeling very
Cocoa-happy at the moment and I'm hoping someone can make me realize
that, as usual with Cocoa, it actually is simpler than I thought.
I want to display something like: "12/16/01 11:59 PM" localized and
accounting for all of the user's time/date preferences. So far, this
is the code I'm using:
id value;
NSCalendarDate *cDate = [NSCalendarDate
dateWithTimeIntervalSince1970:myTime];
NSDictionary *defaultsDict = [[NSUserDefault standardUserDefaults]
dictionaryRepresentation];
value = [cDate descriptionWithCalendarFormat:
[[NSUserDefaults standardUserDefaults] objectForKey:
NSShortDateFormatString] locale:defaultsDict];
value = [value stringByAppendingString:@" "];
value = [value stringByAppendingString:[cDate
descriptionWithCalendarFormat:
[[NSUserDefaults standardUserDefaults]
objectForKey:NSTimeFormatString]
locale:defaultsDict];
So, you can see how this just seems like an absurd amount of code to
do something which should be one simple OS call. Not only that, but
the code above is sketchy at best. For instance, it doesn't even
account for AM/PM. I have tried all sorts of variations, but
interestingly many of the NSUserDefaults strings such as
NSShortTimeDateFormatString do not get properly localized so they are
useless. That appears to be an Apple bug?
In any case, I figure that most people must have figured this out
before me, so if someone has the definitive code for displaying
localized pref-adhering Date/Time, I'd appreciate some pointers.
Thanks!
- -- Will
Will Price, Director of Engineering
PGP Security, Inc.
a division of Network Associates, Inc.
-----BEGIN PGP SIGNATURE-----
Version: PGP 7.5 (Build 112) Beta
iQA/AwUBPB0xjKy7FkvPc+xMEQLCNACfZ+8KENdcdSwp6YtEQpPVSueeB1wAnjMR
qzoDINpS9UM16LqVMWH58ZHD
=2Y46
-----END PGP SIGNATURE-----