2013-05-06 111 views
-1

我有以下代码行将内容或标签设置为日期。从日期对象获取仅一年

cell.birthdayLabel.text = [[GlobalBirthdaysEditor instance] getFormattedDateString:[birthday getDate]]; 

我需要找出在今年等于1604,如果是,不显示它。

我该如何从这个物体拉一年?

所以,基本上在寻找这样的代码:

if (year == 1604) 
    { 
     //set the date without the year 
    } else { 
     cell.birthdayLabel.text = [[GlobalBirthdaysEditor instance] getFormattedDateString:[birthday getDate]]; 
    } 

谢谢!

回答

0
NSDate *currentDate = [[GlobalBirthdaysEditor instance] getFormattedDateString:[birthday getDate]]; 
NSCalendar* calendar = [NSCalendar currentCalendar]; 
NSDateComponents* components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:currentDate]; 
// Get necessary date components 

int month = [components month] //gives you month 
int day = [components day] //gives you day 
int year = [components year] // gives you year 

if(year==1604) 
{ 
    //Do nothing 
} 
else 
{ 
    //Do something 
} 

请进一步的细节

希望这将有助于您访问this page

+0

请不要复制其他答案的单词而不添加任何有价值的内容。如果你发现另一个你认为有用的答案,那么投票结束这个问题的重复,这样我们就不会收到越来越多的相同问题的副本。如果您没有足够的声望投票结束,那么您可以将其标记为主持人操作,或者甚至只是将链接放到其他问题/答案中就对该问题发表评论。 – lnafziger 2013-05-06 17:46:30