2010-11-20 92 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:方法。