在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]]);