Oskar Larsson
Oskar Larsson

Reputation: 155

Save kABPersonBirthdayProperty to the address book without a year?

I'm trying to figure how you can add a birthday to an ABRecordRef without a year (i.e May 5 instead of May 5 1985). Is this possible?

NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"MM/dd/yyyy"];
NSDate *date = [df dateFromString:[result valueForKey:@"birthday"]];

if(date == nil)
{
       df = [[NSDateFormatter alloc] init];
       [df setDateFormat:@"MM/dd"];
       date = [df dateFromString:[result valueForKey:@"birthday"]];
 }

 ABRecordSetValue(person, kABPersonBirthdayProperty, (__bridge CFDateRef)date, nil);

If there's no year attached to the date string it defaults to 1970 but I want it to put it without a year in the address book if that's at all possible.

Anyone knows?

Upvotes: 1

Views: 994

Answers (2)

Dave DeLong
Dave DeLong

Reputation: 243156

You can do this by setting the year of the birthday to 1604, and this will only work with the Gregorian calendar.

See this question and answer for more info.

Upvotes: 1

Stephen
Stephen

Reputation: 739

You can set a birthdate to a contact (through the Contacts app) with only a month/day. The year is then set to 1604, it's just not shown. If before you do your ABRecordSetValue(), you set the year to 1604, the birthdate will show as just "May 5" when viewing the contact in the Contacts app. It appears as though this is the way Apple specifies 'no year'. You will have to manually check the date later if you plan on showing it otherwise you will display the 1604 year.

Upvotes: 3

Related Questions