2010-11-20 161 views

回答

2

你的腳步在這裏:

  • 實例化日曆
  • 構建與去年
  • 的1月1日的日期添加天數 - 1,並建立與
  • 日期獲取您感興趣的組件

小心看看您得到的日期是基於0還是1。

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 

NSDateComponents *comps = [[NSDateComponents alloc] init]; 
[comps setYear:theYear]; 
[comps setMonth:1]; 
[comps setDay:1]; 
NSDate *date = [gregorian dateFromComponents:comps]; 
[comps release]; 

NSDateComponents *compsToAdd = [[NSDateComponents alloc] init]; 
[comps setDay:theDay-1]; 

NSDate *finalDate = [gregorian dateByAddingComponents:compsToAdd date options:0]; 

[compsToAdd release] 

NSDateComponents *interestingComponents = [gregorian components:(NSDayCalendarUnit | NSMonthCalendarUnit) fromDate:finalDate]; 
NSInteger day = [interestingComponents day]; 
NSInteger month = [interestingComponents month]; 

[gregorian release]; 
0

查看NSCalendar-dateByAddingComponents:toDate:options:方法。