UIDatePicker每年添加一个

Bri*_*ker 1 objective-c uidatepicker ios

我有一个非常奇怪和令人困惑的问题.我找不到任何关于它的东西,这似乎是苹果的一个错误,但如果有人建议如何解决这个问题会有所帮助.有些日期在UIDatePicker中添加1年,似乎永远不会低于12月27日.我已经回去几年试图找到一种模式.这里有些例子:

Any date picked >= December 28th in 2014 changes to 2015
>= 29th in 2013 -> 2014
>= 30th in 2012 -> 2013
2011 (issue doesn't occur)
>= 26th in 2010 -> 2011
>= 27th in 2009 -> 2010
>= 28th in 2008 -> 2009
>= 30th in 2007 -> 2008
=  31st in 2006 -> 2007
2005  (issue doesn't occur)
>= 26th in 2004 -> 2005
>= 28th in 2003 -> 2004
>= 29th in 2002 -> 2003
>= 30th in 2001 -> 2002
=  31st in 2000 -> 2001
=  31st in 1999 -> 2000
>= 27th in 1998 -> 1999
>= 28th in 1997 -> 1998
>= 29th in 1996 -> 1997
=  31st in 1995 -> 1996
1994  (issue doesn't occur)
Run Code Online (Sandbox Code Playgroud)

希望这不会太混乱.我似乎无法找到明确的模式,也无法弄清楚如何解决这个问题.这是我用来从选择器中检索日期的代码:

NSString *date;
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"YYYY-MM-dd"];
date = [formatter stringFromDate:datePicker.date];
Run Code Online (Sandbox Code Playgroud)

任何有关这方面的帮助将不胜感激.

小智 8

我不知道它会改变什么,但你可以试试

[formatter setDateFormat:@"yyyy:MM:dd"];

  • 因为“YYYY”代表 [ISO 8601](https://en.wikipedia.org/?title=ISO_8601) 日历,由完整的周组成。由于 365 mod 7 == 1,每个新年的开始都不同,一年有 52 或 53 周长。因此,1月1日、2日、3日可以属于旧的一年,而且12月29日、30日、31日已经可以属于下一年。或者换句话说:如果自然历最后一周的星期四是 12 月 31 日或之前,则该周属于周日历的最后一周。如果它在 1 月 1 日失败,那么整周都属于周历中的新年。 (2认同)