NSDate return “1604” for year value?

淺唱寂寞╮ 提交于 2019-12-20 19:58:08

问题


I'm try to get birth data for contacts but if user does not choose year or the contact come from Facebook and the year or the data it self is hidden so it give me the "1604" value for the year u can see what i mean in the image.


回答1:


Congratulations, you've discovered an implementation detail!

First off, Contacts stores dates as points in time, and it is impossible to store a point in time without a year, because every point in time has a year. Thus, a decision had to be made about how Address Book would store an "unknown" year in a date. It was decided that "1604" would be a fine year, for several reasons:

  1. When you're talking about Address Book, it is unlikely that you'll have anyone in your address book who was born in 1604.
  2. 1604 was a leap year (by the rules of how leap years are calculated), which means that if the person whose birth year you don't know was born on February 29th, then 1604 could handle that, too.
  3. 1604 was before the Julian-Gregorian calendar switch.

Thus, if you're pulling birthdates out of Address Book, you'll need to special-case 1604 as the "birthdate where the year is unknown".

Note: To my knowledge, this is only supported when dealing with the Gregorian calendar. For all other calendars, this behavior is undefined.




回答2:


You can init the year in viewDidLoad.

Or check if the year was not chosen by the user, present error message.



来源:https://stackoverflow.com/questions/14023390/nsdate-return-1604-for-year-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!