关于NSDateFormatter格式化时大小写“yyyy”“YYYY”的不同

这篇博客详细介绍了NSDateFormatter在设置日期格式时,yyyy和YYYY的区别。yyyy表示日历年,而YYYY则表示ISO年-周日历中的年。两者在多数情况下相同,但某些情况下(如年末周跨年)可能会有所不同。此外,文章还列举了其他日期格式的用法,包括月份、日期、小时等的格式化展示。
摘要由CSDN通过智能技术生成

NSDateFormatter

1.使用方法:

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
//显示 年 月 日 时 分 秒 星期 例:2013-10-08 20:58:29 星期二
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss EEEE"];
NSString *string = [formatter stringFromDate: [[NSDate alloc] init]];

2.setDateFormat方法

dateFormat

The date format string used by the receiver.

Declaration

@property(copy) NSString *dateFormat;

Discussion

See Data Formatting Guide for a list of the conversion specifiers permitted in date format strings.

You should only set this property when working with fixed format representations, as discussed in Working With Fixed Format Date Representations. For user-visible representations, you should use the dateStyle and timeStyle properties, or the setLocalizedDateFormatFromTemplate: method if your desired format cannot be achieved using the predefined styles; both of these properties and this method provide a localized date representation appropriate for display to the user.

3.大小写YYYY的区别

It uses yyyy to specify the year component. A common mistake is to use YYYY. yyyy specifies the calendar year whereas YYYY specifies the year (of “Week of Year”), used in the ISO year-week calendar. In most cases, yyyy and YYYY yield the same number, however they may be different. Typically you should use the calendar year.

PS:有英语好的大神的话请帮忙翻译一下,以下翻译来自于Google翻译

它使用yyyy指定年份组件。一个常见的错误是使用YYYY。yyyy指定日历年,而YYYY指定"ISO年-周日历"中使用的年(年中的周)。在大多数情况下,yyyy和yyyy产生相同的数字,但它们可能不同。通常,您应该使用日历年。

 官方链接:Date Formatters

问题复现:

当前年份的最后一个月的最后一天可能显示成下一年的最后一个月的最后一天(实际上就是年份显示有问题。例:应该显示2021年,实际显示2022年)

4.NSDateFormatter格式汇总

4.1 正常显示

[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss EEEE"];

2021-01-01 18:00:00 星期五

4.2 yyyy -> yy 年份只显示后两位

[dateFormatter setDateFormat:@"yy-MM-dd HH:mm:ss EEEE"];

21-01-01 18:00:00 星期五

4.3 MM -> M 和 dd -> d 当月份和日期是双数时正常显示,是单数时只显示一位数

[dateFormatter setDateFormat:@"yyyy-M-d HH:mm:ss EEEE"];

2021-1-1 18:00:00 星期五

4.4 dd -> DD 显示今天是今年的第几天

[dateFormatter setDateFormat:@"yyyy-MM-DD HH:mm:ss EEEE"];

2021-12-365 18:00:00 星期五

4.5 HH -> H 和 mm -> m 和 ss -> s 当时和分和秒是双数时正常显示,是单数时只显示一位数

[dateFormatter setDateFormat:@"yyyy-MM-dd H:m:s EEEE"];

2021-01-01 18:0:0 星期五

4.6 HH -> hh 时间从24小时制变为12小时制

[dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss EEEE"];

2021-01-01 06:00:00 星期五

4.7 加a,区分上午下午

[dateFormatter setDateFormat:@"yyyy-MM-dd a hh:mm:ss EEEE"];

2021-01-01 下午 06:00:00 星期五

4.8  EEEE -> EE 星期几遍为周几

[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss EE"];

2021-01-01 18:00:00 周五

4.9 - -> / 中间的-分隔符变为/分隔符

[dateFormatter setDateFormat:@"yyyy/MM/dd a hh:mm:ss EE"];

2021/01/01 下午 06:00:00 周五

欢迎补充添加~~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

敛柒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值