NSDateFormatter NSString 带 NSDdate 的日期后缀

发布于 2024-09-13 14:04:16 字数 341 浏览 1 评论 0原文

我尝试仅使用 NSDateFormatter 将日期/时间的字符串表示形式格式化为 NSDate 对象。问题是我不知道如何在格式中允许序数后缀。

假设我有一个字符串 “2010 年 8 月 11 日星期三晚上 8:00”

我应该使用哪一行 NSDate dateFormat?

就像“EEEE, MMM dd'th' yyyy h:mm a” 一样,但是这只适用于以“th”结尾的序数天,而我需要一种允许第一个的格式、第二个、第三个、第四个、第五个等等。

这似乎是格式字符串中的通配符来完成此操作。我尝试过一些事情,例如: % * ?

I'm trying to use only a NSDateFormatter to format string representation of a date/time to an NSDate object. The issue is that I can't figure out how to allow the ordinal suffixes in the format.

So lets say I have a string
"Wednesday, August 11th 2010 8:00 PM"

What one line NSDate dateFormat should I use?

Like "EEEE, MMM dd'th' yyyy h:mm a" would work, but that will only work for ordinal days ending in 'th', whereas i need a single format that will allow for 1st, 2nd, 3rd, 4th 5th etc.

It seems like a wildcard character in the format string to accomplish this. I've tried a few things like: % * ?

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

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

发布评论

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

评论(2

随风而去 2024-09-20 14:04:16

这对于 NSDateFormatter 显然是不可能的。

This is apparently not possible with the NSDateFormatter.

囚你心 2024-09-20 14:04:16

您想像这样使用 NSDateFormatter:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];    
[dateFormatter setDateStyle: NSDateFormatterLongStyle];
NSDate *date = [dateFormatter dateFromString:dateString];
[dateFormatter release];

NSDateFormatterLongStyle 或 NSDateFormatterFullStyle 应该可以为您提供您正在寻找的结果。

You want to use an NSDateFormatter like so:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];    
[dateFormatter setDateStyle: NSDateFormatterLongStyle];
NSDate *date = [dateFormatter dateFromString:dateString];
[dateFormatter release];

Either NSDateFormatterLongStyle or NSDateFormatterFullStyle should get you the results you're looking for.

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