views:

20

answers:

1

Hey there everyone!

I'm creating an app that needs to run on 10.4 and above. Having a date string that is formatted properly is crucial. When I run my program in 10.5 debug everything is good; I get a date which is exactly how I want it:

 Jul 13, 2010 16

the 16 being the hour without the minutes, which I specifically need. Then when I run it in 10.4 I get this:

 07/13/10 

My code to produce this date looks like this:

  NSDateFormatter *format = [[NSDateFormatter alloc] init];
  [format setDateFormat:@"MMM dd, yyyy HH"];

  NSDate *newNow = [[NSDate alloc] init];
  NSString *dateString = [format stringFromDate:newNow];

After reading some relevant documentation (link below), it seems like 10.4 has a unique way to to format that is different then 10.3, 10.5 and 10.6. To quote the link:

"The format string uses the format patterns from the Unicode standard (this reference is to version tr35-6 for Mac OS X v10.5; for Mac OS X v10.4 use version tr35-4)."

I've gone through the documentation and it seems like my Unicode format is okay, but I'm still getting this odd result.

Any help?

http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html#//apple_ref/doc/uid/TP40002369-SW1

A: 

I think you need to set the formatter behavior to NSDateFormatterBehavior10_4 using +[NSDateFormatter setDefaultFormatterBehavior:] or -[NSDateFormatter setFormatterBehavior:].

EDIT: my reasoning is based on the comment in the NSDateFormatter overview saying:

By default, on Mac OS X v10.4 instances of NSDateFormatter have the same behavior as they did on Mac OS X versions 10.0 to 10.3. On Mac OS X v10.5 and later, NSDateFormatter defaults to the 10.4+ behavior.

JWWalker
That totally did the trick. For anyone else that has to try this I would add that you need to place // [NSDateFormatter setDefaultFormatterBehavior:NSDateFormatterBehavior10_4]; before everything else, including the alloc and init stuff.
Eric Brotto