iphone NSDate 与 NSDateFormatter 问题

发布于 2024-09-10 01:18:04 字数 560 浏览 3 评论 0原文

我想做的就是获取一个 NSString ,其中包含当前日期( NOW )的值,

格式如下:

2010 年 7 月 14 日晚上 8:20

与 iPhone 的本机邮件应用程序完全相同。

我正在使用以下代码来执行此操作:

 NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
 [dateFormat setDateFormat:@"EEE, dd MMMM yyyy HH:mm:ss ZZ"];
 [dateFormat setDateStyle:NSDateFormatterFullStyle];
 NSString *dateString = [dateFormat stringFromDate:[NSDate date]];

但结果仍然是:

2010 年 7 月 14 日星期三

有谁知道如何让它正常工作吗?

感谢您的回答..

All i am trying to do, is to get an NSString with the value of the current date ( NOW )

with this format:

7/14/10 8:20 PM

Exactly like the native mail app of the iPhone.

i am using the following code to do it:

 NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
 [dateFormat setDateFormat:@"EEE, dd MMMM yyyy HH:mm:ss ZZ"];
 [dateFormat setDateStyle:NSDateFormatterFullStyle];
 NSString *dateString = [dateFormat stringFromDate:[NSDate date]];

but the still the result is:

Wednesday, July 14, 2010

Does anyone have any idea of how to get this to just WORK ??

Appreciate your answers..

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

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

发布评论

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

评论(3

凉栀 2024-09-17 01:18:04

setDateStyle: 覆盖使用 setDateFormat: 设置的自定义格式字符串。

setDateStyle: overrides the custom format string set with setDateFormat:.

带刺的爱情 2024-09-17 01:18:04

除此之外,您应该根据 Nikolai 的回答首先调用 -setDateStyle: ,您的格式字符串与您的示例不匹配,该示例将由以下内容生成:

[dateFormat setDateFormat:@"M/d/yy h:mm a"];

请参阅 Unicode 日期格式模式 了解更多详细信息。

Besides that you should call -setDateStyle: first as per Nikolai answer, your formatting string doesn't match your example, which would be produced by e.g. the following:

[dateFormat setDateFormat:@"M/d/yy h:mm a"];

See the Unicode date format patterns for more details.

云仙小弟 2024-09-17 01:18:04

请参阅 Apple 的 日期格式文档。

我还没有测试过,但尝试一下

 [dateFormat setDateFormat:@"MM/DD/yy"]; 

See Apple's Date Formatting docs.

I haven't tested it, but try

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