Comments (4)
Thanks for the feedback. Do you have a fix that you'd like to suggest?
from mbcalendarkit.
The source of the bug is the use of dateBySubtractingDays, passing it the number of days in the current month. Since all months don't have the same number of days, you end up with problems.
I had my own category on NSDate that performed various date calculations, so I used them to get around this. Here is that solution. Instead of the 3 lines noted above, use this:
date = [[date dateAtStartOfMonth] dateByAddingNumberOfMonths:1];
where dateAtStartOfMonth and dateByAddingNumberOfMonths are defined like this:
- (NSDate*) dateAtStartOfMonth
{
NSDate* startOfMonth = nil;
NSTimeInterval interval = 0;
BOOL ok = [[NSCalendar currentCalendar] rangeOfUnit:NSMonthCalendarUnit startDate:&startOfMonth interval:&interval forDate:self];
if (ok)
{
if ([self isEqualToDate:startOfMonth])
return self;
else
return startOfMonth;
}
return NULL;
}
- (NSDate*) dateByAddingNumberOfMonths:(NSInteger)months
{
NSDateComponents* components = [[NSDateComponents alloc] init];
[components setMonth:months];
NSDate* newDate = [[NSCalendar currentCalendar] dateByAddingComponents:components toDate:self options:0];
return newDate;
}
I didn't delve into it enough to figure out if there was a fix using the NSCalendar+DateManipulation category.
from mbcalendarkit.
Your comment noting that "all months don't have the same number of days" was on target. I believe that ticking the month forward first before subtracting the days fixes the problem. The logic of "subtract x days of the month except one" should work, but x days of the month needs be calculated using the target month, not the month we're jumping from.
The original code looked like this, as you noted above:
NSUInteger day = [[self calendar] daysInDate:date];
date = [[self calendar] dateByAddingMonths:1 toDate:date]; // Add a month
date = [[self calendar] dateBySubtractingDays:day-1 fromDate:date]; // Go to the first of the month
The reality is that it should look like this:
date = [[self calendar] dateByAddingMonths:1 toDate:date]; // Add a month
NSUInteger day = [[self calendar] daysInDate:date]; // Only then go to the first of the next month.
date = [[self calendar] dateBySubtractingDays:day-1 fromDate:date];
See the difference? It seems to solve the problem.
from mbcalendarkit.
Fixed in 8cdc4ed.
from mbcalendarkit.
Related Issues (20)
- Bring `CustomCellProvider` to the HeaderView
- Cell Transition Animation Bug HOT 1
- Unit Test Cell Width Code
- CalendarView doesn't take in account firstDayOfWeek HOT 10
- Disable dragging on CustomCellProviding HOT 5
- Performance Profiling HOT 1
- Allow Custom Next/Back Images
- Month to Week Transition observable gap
- how refresh the data HOT 1
- how avoid show the tab bar HOT 1
- how to use background color in every sunday?
- Hello Project's Owner
- How to show more than one event in single date for MBCalendarKit for swift 4 HOT 1
- Changed it to NSCalendarIdentifierIslamic not working
- How to achieve calendar color customization? HOT 1
- How to change colors of calendar header, arrows, and selected cell?
- How to hide rows in Month display mode.
- Installing mBCalendarKit via Cocoapods is getting failed. HOT 1
- Calendar week view - pressing forwards/backwards buttons sometimes skips a week
- Tab Bar Text...
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mbcalendarkit.