Question

NSCalendar *calendar= [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSCalendarUnit unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
NSDateComponents *dateStart = [calendar components:unitFlags fromDate:start];
quarterStart = [dateStart quarter];
NSDateComponents *dateEnd = [calendar components:unitFlags fromDate:end];
quarterEnd = [dateEnd quarter];

Having two NSDate start and end and extracting start quarter and end quarter i need to know the differences in quarters between these 2 dates.

I know that i could divide the total days / 90 days but i'm looking at another solution having only these data.

Any hints ?

Was it helpful?

Solution 2

I've solved using that, cause the other suggested answer not works for an Apple's bug on quarter Unit.

numberOfQuarters = (4 - startQuarter) + ((endYear -1 - startYear) * 4) + endQuarter;

OTHER TIPS

You should try:

NSDateComponents *result = [[NSCalendar currentCalendar] components:NSQuarterCalendarUnit fromDate:start toDate:end options:0];
NSInteger numberOfQuaters = [result quarter];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top