在 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/