Printing an NSDate
-
I want to print a date on iOS 5.0.1 ignoring the locale.
(this is for logging - not for showing strings to users)
I assume that NSDate has no sufficient parameters to control the output.
So I tried to use NSDateFormatter.
The desired output is something like:
NSString *template = @"HH:mm:ss EEE dd. MMM yyyy zzz";
NSString *dateFormat = [ NSDateFormatter dateFormatFromTemplate: template options: 0 locale: nil ];
NSDateFormatter *dateFormatter = [ [ NSDateFormatter alloc ] init ];
[ dateFormatter setDateFormat: dateFormat ];
NSString *dateString = [ dateFormatter stringFromDate: someDate ];
[ dateFormatter release ];
1. problem:
The date gets output as year, month, day which is NOT what I specified.
2. problem:
The output is: date time, NOT time date as requested.
What am I doing wrong?
Kind regards,
Gerriet. -
On Jan 19, 2012, at 7:41 AM, Gerriet M. Denkmann wrote:
> I want to print a date on iOS 5.0.1 ignoring the locale.
> (this is for logging - not for showing strings to users)
>
> I assume that NSDate has no sufficient parameters to control the output.
> So I tried to use NSDateFormatter.
>
> The desired output is something like:
> NSString *template = @"HH:mm:ss EEE dd. MMM yyyy zzz";
>
> NSString *dateFormat = [ NSDateFormatter dateFormatFromTemplate: template options: 0 locale: nil ];
> NSDateFormatter *dateFormatter = [ [ NSDateFormatter alloc ] init ];
> [ dateFormatter setDateFormat: dateFormat ];
> NSString *dateString = [ dateFormatter stringFromDate: someDate ];
> [ dateFormatter release ];
>
> 1. problem:
> The date gets output as year, month, day which is NOT what I specified.
>
> 2. problem:
> The output is: date time, NOT time date as requested.
>
> What am I doing wrong?
>
> Kind regards,
>
> Gerriet.
Maybe this is what you are looking for:
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat: @"HH:mm:ss EEE dd. MMM yyyy zzz"];
NSString* dateString = [dateFormatter stringFromDate: [NSDate date]];
[dateFormatter release];
NSLog(@"\n%@", dateString);
prints:
11:31:42 Thu 19. Jan 2012 GMT+01:00
The documentation could be a bit more clear in this regard, but please re-read it ;)
Andreas -
On Jan 18, 2012, at 10:41 PM, "Gerriet M. Denkmann" <gerriet...> wrote:
> The desired output is something like:
> NSString *template = @"HH:mm:ss EEE dd. MMM yyyy zzz";
>
> NSString *dateFormat = [ NSDateFormatter dateFormatFromTemplate: template options: 0 locale: nil ];
> NSDateFormatter *dateFormatter = [ [ NSDateFormatter alloc ] init ];
> [ dateFormatter setDateFormat: dateFormat ];
> NSString *dateString = [ dateFormatter stringFromDate: someDate ];
> [ dateFormatter release ];
>
> 1. problem:
> The date gets output as year, month, day which is NOT what I specified.
>
> 2. problem:
> The output is: date time, NOT time date as requested.
>
> What am I doing wrong?
You used the +dateFormatFromTemplate: method, which is specifically designed and documented to reorder the date components in the current locale. If you don't want this behavior, don't ask for it; use -initWithDateFormat:allowsNaturalLanguage: instead.
You should also call -setLocale: to make sure you get the same symbols and values for all fields. You probably want [NSLocale systemLocale].
--Kyle Sluder -
On Jan 19, 2012, at 2:39 AM, <cocoa-dev-request...> wrote:
> Date: Thu, 19 Jan 2012 13:41:54 +0700
> From: "Gerriet M. Denkmann" <gerriet...>
> Subject: Printing an NSDate
> To: <cocoa-dev...>
> Message-ID: <CD59692A-8452-4D4B-AD12-E49636F8DC3E...>
> Content-Type: text/plain; charset=us-ascii
>
> I want to print a date on iOS 5.0.1 ignoring the locale.
> (this is for logging - not for showing strings to users)
>
> I assume that NSDate has no sufficient parameters to control the output.
> So I tried to use NSDateFormatter.
>
> The desired output is something like:
> NSString *template = @"HH:mm:ss EEE dd. MMM yyyy zzz";
>
> NSString *dateFormat = [ NSDateFormatter dateFormatFromTemplate: template options: 0 locale: nil ];
> NSDateFormatter *dateFormatter = [ [ NSDateFormatter alloc ] init ];
> [ dateFormatter setDateFormat: dateFormat ];
> …
A "template" for [ NSDateFormatter dateFormatFromTemplate: …] is not a format; rather, it is just treated as a list of which fields are desired in a format. The order and formatting of those fields in the template is ignored. The whole point of dateFormatFromTemplate is to take a request for those fields and turn it into an actual locale-appropriate format containing those fields in the locale-appropriate order, with locale-appropriate formatting. If you want to set "HH:mm:ss EEE dd. MMM yyyy zzz" itself as the format, then just pass that directly to [ dateFormatter setDateFormat: … ].
- Peter E


