我有以下代码行将内容或标签设置为日期。
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 :(得分: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了解更多详情
希望它会对你有所帮助。