问题
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:
- When you're talking about Address Book, it is unlikely that you'll have anyone in your address book who was born in 1604.
- 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.
- 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