相对日期格式,过去日期的输出?

编程入门 行业动态 更新时间:2024-10-25 01:24:46
本文介绍了相对日期格式,过去日期的输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用 - [NSDateFormatter setDoesRelativeDateFormatting:] 将日期显示为今天或昨天。我只是查看过去的日期,但很好奇我会为英国本地化的选项。

只是

  • 今天
  • 昨天
<

  • 前一天
$

解决方案

b $ b

是否可能的输出列在任何地方,所以我可以了解正确显示它们所需的屏幕空间?

是的。当您运行以下程序时,它们会列在控制台窗口中:

#import< Foundation / Foundation.h> int main(int argc,const char * argv []) { @autoreleasepool { NSDateFormatter * formatter = [NSDateFormatter alloc] init]; [formatter setDateStyle:NSDateFormatterFullStyle]; [formatter setDoesRelativeDateFormatting:YES]; NSCalendar * cal = [[NSLocale currentLocale] objectForKey:NSLocaleCalendar]; NSDateComponents * minusOneDay = [[NSDateComponents alloc] init]; [minusOneDay setDay:-1]; NSDate * today = [NSDate date]; NSDate * date = [NSDate date]; while(1> [[cal components:NSYearCalendarUnit fromDate:date toDate:today options:0] year]){ NSLog(@%@ formatter stringFromDate:date]); date = [cal dateByAddingComponents:minusOneDay toDate:date options:0]; } } return 0; }

在我的语言环境中,列表似乎只是Tomorrow 和昨天。

I am looking at using -[NSDateFormatter setDoesRelativeDateFormatting:] to present dates as "Today" or "Yesterday". I am only looking at dates in the past but am curious what options I would see localised for the UK.

Just

  • "Today"
  • "Yesterday"

or anything more convoluted like

  • "The day before yesterday"

Are the possible outputs listed anywhere so I can get an idea of the screen space needed to correctly display them?

解决方案

Yes. They are listed in your console window when you run the following program:

#import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepool { NSDateFormatter * formatter = [[NSDateFormatter alloc] init]; [formatter setDateStyle:NSDateFormatterFullStyle]; [formatter setDoesRelativeDateFormatting:YES]; NSCalendar * cal = [[NSLocale currentLocale] objectForKey:NSLocaleCalendar]; NSDateComponents * minusOneDay = [[NSDateComponents alloc] init]; [minusOneDay setDay:-1]; NSDate * today = [NSDate date]; NSDate * date = [NSDate date]; while( 1 > [[cal components:NSYearCalendarUnit fromDate:date toDate:today options:0] year] ){ NSLog(@"%@", [formatter stringFromDate:date]); date = [cal dateByAddingComponents:minusOneDay toDate:date options:0]; } } return 0; }

In my locale, the list seems to just be "Tomorrow", "Today", and "Yesterday".

更多推荐

相对日期格式,过去日期的输出?

本文发布于:2023-10-23 00:04:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1519139.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:日期   格式

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!