我想从指定日期获得四分之一值。(*日期变量)
所以。我制作了以下代码并运行。但是,四分之一值为零。
为什么季度值为零?哪里有问题?
请高人指教。
NSDateComponents *comp1 = [[NSDateComponents alloc] init];
[comp1 setYear:2012];
[comp1 setMonth:6];
[comp1 setDay:1];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *date = [gregorian dateFromComponents:comp1]; <== want to get a quarter value.
unsigned int unitFlags = NSYearCalendarUnit | NSQuarterCalendarUnit | NSMonthCalendarUnit | NSWeekOfYearCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *comp2 = [gregorian components:unitFlags fromDate:date];
NSLog(@"year : %5d",[comp2 year]);
NSLog(@"quarter : %5d",[comp2 quarter]);
NSLog(@"month : %5d",[comp2 month]);
NSLog(@"weekofYear : %5d",[comp2 weekOfYear]);
NSLog(@"day : %5d",[comp2 day]);
结果:
2012-07-24 03:44:14.776 dateTest[1228:1307] year : 2012
2012-07-24 03:44:14.776 dateTest[1228:1307] quarter : 0
2012-07-24 03:44:14.777 dateTest[1228:1307] month : 6
2012-07-24 03:44:14.777 dateTest[1228:1307] weekofYear : 22
2012-07-24 03:44:14.778 dateTest[1228:1307] day : 1