I want to get a quarter value in NSDateComponents class

跟風遠走 提交于 2019-11-27 16:12:47

It seems that there is an issue with the NSQuarterCalendarUnit being ignored (haven't verified, but did see a bug reported). Anyway, it is simple enough to get around... Just use the NSDateFormatter class.

// After your NSDate *date, add...
NSDateFormatter *quarterOnly = [[NSDateFormatter alloc]init];
[quarterOnly setDateFormat:@"Q"];

int quarter = [[quarterOnly stringFromDate:date] intValue];

// Then change the quarter NSLog too
NSLog(@"quarter :    %5d", quarter);

Should work fine...

Dec 16 2014

How about simply:

([comp2 month]-1)/3+1;

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!