gpt4 book ai didi

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

转载 作者:技术小花猫 更新时间:2023-10-29 10:36:36 24 4
gpt4 key购买 nike

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com