如何使 NSDateFormatter 接受一种格式的输入并以另一种格式显示它?

发布于 2024-09-24 10:59:29 字数 323 浏览 1 评论 0原文

我有一个表格视图,它使用 NSDateFormatter 显示日期,并使用完整日期样式对其进行格式化(今天将格式化为“Friday, September 24, 2010”或德语“Freitag, 24. September 2010”) ”)。但是,在编辑日期时,格式化程序会强制用户以相同的格式键入日期。如何使格式化程序在输入时接受不同格式的日期?理想情况下,格式化程序应该使用一些启发式方法来自动检测输入的格式。

我尝试在代码中设置 lenient 属性,但这根本不会改变观察到的行为。

许多程序会自动检测输入的日期格式,因此这必须是可能的。或者他们都使用自己的实现?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

秋意浓 2024-10-01 10:59:29

也许数据格式化指南可以帮助:

v10.4行为模式允许更多
可配置性和更好
本土化;你应该用它来
任何新项目,最好是升级
使用 v10.0 的任何现有代码
使用 v10.4 行为的行为。

为了向后二进制兼容性,
默认行为
Mac OS X 版本上的 NSDateFormatter
10.4 是 v10.0 行为。但是,在 Mac OS X 版本 10.5 及更高版本上,
默认为 v10.4 行为。你可以
设置所有的默认行为
NSDateFormatter 的实例
v10.4 通过调用类的行为
方法,setDefaultFormatterBehavior:
与论证
NSDateFormatterBehavior10_4。你可以
还可以设置任何实例的行为
单独调用
setFormatterBehavior:.

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:.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文