ios - 在设置 doesRelativeDateFormatting = YES 时,stringFromDate 返回 nil

标签 ios iphone nsdateformatter

在 NSDateFormatter 文档中,

If a date formatter uses relative date formatting, where possible it replaces the date component of its output with a phrase—such as “today” or “tomorrow”—that indicates a relative date.

我正在使用以下代码:

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = [NSDateFormatter dateFormatFromTemplate:@"EEEE dMMMM',' hma"
                                                       options:0
                                                        locale:[NSLocale currentLocale]];
formatter.doesRelativeDateFormatting = YES;
NSLog(@"Date: %@", [formatter stringFromDate:[NSDate date]]);

它记录日期:

但是在注释掉 formatter.doesRelativeDateFormatting = YES; 时输出是正确的。

日期:8 月 19 日星期二下午 1:18

我对 doesRelativeDateFormatting 的理解有误吗?

最佳答案

嗨,当我像这样修改代码时。它有效..

 NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
 formatter.dateFormat = [NSDateFormatter dateFormatFromTemplate:@"EEEE dMMMM',' hma"
                                                       options:0
                                                        locale:[NSLocale currentLocale]];
 [formatter setDateStyle:NSDateFormatterFullStyle];
 [formatter setTimeStyle:NSDateFormatterShortStyle];
 formatter.doesRelativeDateFormatting = YES;
  NSLog(@"Date: %@", [formatter stringFromDate:[NSDate date]]);

关于ios - 在设置 doesRelativeDateFormatting = YES 时,stringFromDate 返回 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25378188/

相关文章:

ios - UIButton - 当调用触摸重复时防止调用触摸事件

android - cordova 7.0.1 iOS 构建失败

ios - 替换 html 字符串中的特定单词

ios - 即使应用程序关闭也运行时钟或计时器

swift - NSDateFormatter 不适用于实际设备

ios - 如何在我的应用程序中添加 PLCrashReporter?

ios - 是否显示 UIView?

iphone - 如何使用代码在 ios 应用程序中录制屏幕视频?

ios - 如何保护项目中的 .plist 和其他 .xml 文件?

xcode - (Swift) 使用 NSDataFormatter 和中等样式字符串来获取日期