This is a migrated thread and some comments may be shown as answers.

Get visible date range in month view

1 Answer 125 Views
Calendar
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Mathieu
Top achievements
Rank 1
Mathieu asked on 03 Sep 2015, 08:07 AM

Hi,

 I have a calendar with mode = TKCalendarViewModeMonth and presenter.equalWeekNumber = NO. How can I find the first and last date which is currently displayed by this calendar? Since there is overlap from the last and into the next month displayed, this is hard to predict. I also found the dateForRow:(NSInteger)row col:(NSInteger)col method, which I can probably use to get the first date (since this is always row 0 col 0). But how can I get the last date? I can't tell for sure how many rows there are (it could be 4, 5 or 6).

I need to know the full date range up front, so dealing with this stuff in updateVisualsForCell or the like doesn't help.

1 Answer, 1 is accepted

Sort by
0
Accepted
Jack
Telerik team
answered on 03 Sep 2015, 11:52 AM
Hi Mathieu,

Yes, your suggestion is correct. The dateForRow:col: method will fit in this case. You can use the following code to get the first and last dates currently displayed in TKCalendar:
TKCalendarMonthPresenter *presenter = (TKCalendarMonthPresenter*)self.calendarView.presenter;
 
NSDateComponents *comps = [self.calendarView.calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay
                                                        fromDate:self.calendarView.displayedDate];
comps.month += 1;
comps.day = 0;
NSDate *lastDayInMonth = [self.calendarView.calendar dateFromComponents:comps];
NSInteger weeksInMonth = 0;
if (presenter.equalWeekNumber) {
    weeksInMonth = 6;
}
else {
    weeksInMonth = [self.calendarView.calendar components:NSCalendarUnitWeekOfMonth fromDate:lastDayInMonth].weekOfMonth;
}
NSInteger daysInWeek = [self.calendarView.calendar rangeOfUnit:NSCalendarUnitWeekday inUnit:NSCalendarUnitWeekOfMonth forDate:self.calendarView.displayedDate].length;
NSDate *first = [presenter dateForRow:0 col:0];
NSDate *last = [presenter dateForRow:weeksInMonth-1 col:daysInWeek-1];
NSDateFormatter *formatter = [NSDateFormatter new];
formatter.dateFormat = @"dd/MM/yyyy";
NSLog(@"%@ - %@", [formatter stringFromDate:first], [formatter stringFromDate:last]);

I hope it helps.

Regards,
Jack
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
Calendar
Asked by
Mathieu
Top achievements
Rank 1
Answers by
Jack
Telerik team
Share this question
or