开发者

How can I make a NSDateFormatter accept input in one format and display it in another?

I have a table view that displays dates using a NSDateFormatter to format them using the Full date style (today would be formatted as "Friday, September 24, 2010" or in German "Freitag, 24. September 2010"). But when editing the date the formatter forces the user to type the date in the same format. How can I make the formatter accept dates in different formats on input? Ideally the formatter should use some heuristic to automatically detect the entered format.

I tried setting 开发者_开发百科the lenient property in code, but this doesn’t change the observed behavior at all.

Lots of programs automatically detect the inputted date format, so this has to be possible. Or are they all using their own implementation?


Maybe this section of the Data Formatting Guide can help:

The v10.4 behavior mode allows more configurability and better localization; you should use it for any new projects, and ideally upgrade any existing code that uses v10.0 behavior to use v10.4 behavior.

For backwards binary compatibility, the default behavior for NSDateFormatter on Mac OS X version 10.4 is the v10.0 behavior. On Mac OS X version 10.5 and later, however, the default is the v10.4 behavior. You can set the default behavior of all instances of NSDateFormatter to the v10.4 behavior by invoking the class method, setDefaultFormatterBehavior: with the argument NSDateFormatterBehavior10_4. You can also set the behavior for any instance individually by invoking setFormatterBehavior:.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜