ios - 是否可以在 NSDateFormatter 的格式字符串中添加自定义文本?

标签 ios objective-c nsdateformatter

假设我希望日期看起来像这样:

|1988|December|30|

如何将这些添加到 dateFormatter 或就此而言让格式像这样:

30 in the month of December in the year of 1998

现在是 1988 年、12 月和 30 日,我想使用标准格式,但我希望我放入的文本也能伴随它们。

特别是在上述情况下,格式和管道仅与日期或月份的日期格式相邻,格式和管道之间没有空格。

仅通过设置格式就可以做到这一点吗?

最佳答案

例如,您可以在日期格式中插入任意文本(用单引号引起来)。

NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
[fmt setDateFormat:@"dd' in the month of 'MMMM' in the year of 'yyyy"];
NSString *s = [fmt stringFromDate:[NSDate date]];

结果:

09 in the month of July in the year of 2013

关于ios - 是否可以在 NSDateFormatter 的格式字符串中添加自定义文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17542564/

相关文章:

objective-c - 委托(delegate)对象在设置之前存在于目标 View Controller 中,但在设置之后为 0x000..。为什么?

iphone - AppDelegate 属性在 viewController 之间被重置

iphone - 为什么 fabs 返回整数?

iphone - 在某些单元格上设置 UITableViewCell 附件类型,但不是全部

ios - 为什么 NSFormatter dateFromString 偶尔会返回 nil?

swift - 在 Swift 中查找日期之间的时间

ios - 我如何从 javascript 调用 Objective C 代码

ios - 启用/禁用 UISegmentedControl

ios - 如何在iOS 8和更高版本中处理RTL语言的后退按钮图像

swift - 为什么我的 DateFormatter .date(from : String) return nil