How to get currently displayed month and year in format "MM/yyyy" in JTCalendar Objective-C

466 Views Asked by At

I want to get the currently displayed month with year in the format "MM/yyyy" eg. 03/2017 in JTCalendar.

Here is my code which is called in a method getCurrentMonthAndYear

NSDateFormatter *monthYearFormatter;

[_calendarManager reload];

if (!monthYearFormatter) {
    monthYearFormatter = [NSDateFormatter new];
    monthYearFormatter.dateFormat = @"MM/yyyy";
}

NSString *monthYear = [monthYearFormatter stringFromDate:[_calendarManager date]];

Not sure about [_calendar date] because this method doesn't return exact month and year displayed in JTCalendar when navigating from one month to another.

I've also tried

- (void)calendar:(JTCalendarManager *)calendar prepareMenuItemView:(UIView *)menuItemView date:(NSDate *)date method, there i was getting 3 months, one previous and one after the month which is displayed.

Any help is appreciated. Thank you.

Edit 2:

Tried calling the getCurrentMonthAndYear method inside - (void)calendar:(JTCalendarManager *)calendar prepareMenuItemView:(UIView *)menuItemView date:(NSDate *)date. This however returns the exact month and year which is being viewed, but the method is called 3 times. So this hits my API 3 times.

1

There are 1 best solutions below

4
iCoderz Developers On BEST ANSWER

Updated code:

  • (void)calendarDidLoadPreviousPage {

    [self getCurrentMonthAndYear];

    NSLog(@"Prev Date: %@", self.calendar.currentDate); }

Let me know if it is working.