Re: Right Way to Display Dates+Times
Re: Right Way to Display Dates+Times
- Subject: Re: Right Way to Display Dates+Times
- From: Greg Titus <email@hidden>
- Date: Sun, 16 Dec 2001 17:39:43 -0800
On Sunday, December 16, 2001, at 03:43 PM, Will Price wrote:
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];
This this:
NSString *value;
NSCalendarDate *cDate = [NSCalendarDate
dateWithTimeIntervalSince1970:myTime];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
value = [cDate descriptionWithCalendarFormat:[NSString
stringWithFormat:@"%@ %@ %%p", [defaults objectForKey:
NSShortDateFormatString], [defaults objectForKey:NSTimeFormatString]]];
Sticks all the parts together at once, and includes the localized AM/PM
designation. You don't need the locale: arguments that you used because
the user's locale is the default if you don't specify one...
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?
Guess so. Presumably if that worked, that would be the way to go.
Hope this helps,
--Greg