ios – 设置didRelativeDateFormatting = YES时,stringFromDate返回nil

前端之家收集整理的这篇文章主要介绍了ios – 设置didRelativeDateFormatting = YES时,stringFromDate返回nil前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在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分

我是否理解了相关日期格式错误

解决方法

嗨,当我修改这样的代码.有用..
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]]);
原文链接:https://www.f2er.com/iOS/329587.html

猜你在找的iOS相关文章