Get visible date range in month view

2 posts, 1 answers
  1. Mathieu
    Mathieu avatar
    11 posts
    Member since:
    Nov 2014

    Posted 03 Sep 2015 Link to this post

    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.

  2. Answer
    Jack
    Admin
    Jack avatar
    2335 posts

    Posted 03 Sep 2015 Link to this post

    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
  3. DevCraft banner
Back to Top