我有一年中的一天,例如346。 我想让NSDate到现在的月份和日期。
答案 0 :(得分:2)
您的步骤:
注意您获得的日期是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];
答案 1 :(得分:0)
请参阅-dateByAddingComponents:toDate:options:
的{{1}}方法。