How to get the month name with year in JTCalendar?

1.1k Views Asked by At

I am using JTCalendar. I want to get the month for which calendar in shown. As of now no month name is shown in calendar. I have used below code to show calendar.

  calendarManager = JTCalendarManager()
  calendarManager?.delegate = self
  var calender = calendarManager?.dateHelper.calendar()
  calender?.firstWeekday = 1
  calendarManager?.contentView = calendarContentView
  calendarManager?.setDate(todayDate)

Please guide how can I show month name with year.

4

There are 4 best solutions below

2
Aakash On

Just set your month's UIView class to "JTCalendarMenuView" and then, you can use the JTCalendarDelegate Methods as:

    - (UIView *)calendarBuildMenuItemView:(JTCalendarManager *)calendar
{
    UILabel *label = [UILabel new];

    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont systemFontOfSize:16];

    return label;
}

- (void)calendar:(JTCalendarManager *)calendar prepareMenuItemView:(UILabel *)menuItemView date:(NSDate *)date
{
    static NSDateFormatter *dateFormatter;
    if(!dateFormatter){
        dateFormatter = [NSDateFormatter new];
        dateFormatter.dateFormat = @"MMMM yyyy";
    }
    menuItemView.text = [dateFormatter stringFromDate:date];
}

This will give you text as "December 2016" (for eg.)

0
Raj D On

In addition to @Aakash's answer, menuItemView is actually a UIView. So it does not have .text property. Instead use this line, [(UILabel*) menuItemView setText:[dateFormatter stringFromDate:date]];

0
Ahtazaz On

Swift

just add this function.

   func calendar(_ calendar: JTCalendarManager!, prepareMenuItemView menuItemView: UIView!, date: Date!) {
    for view in menuItemView.subviews {
        view.removeFromSuperview()
    }
    let label1 = UILabel(frame: CGRect(x: 80, y: 60, width:400, height: 21))
    label1.center = CGPoint(x: 130, y: 20)
    label1.textColor = UIColor.red
    label1.textAlignment = NSTextAlignment.center
    let dateFormatter1 = DateFormatter()
    dateFormatter1.dateFormat = "MMMM, yyyy";
    let mydt = dateFormatter1.string(from: date)
    label1.text = mydt
    menuItemView.addSubview(label1)
}
0
Rajesh Loganathan On

Swift 4+:

Simple solution

var getMonthYearFromDate: String? 
{
     let dateFormatter = DateFormatter()
     dateFormatter.dateFormat = "MMMM, yyyy"
     let strMonth = dateFormatter.string(from: self)
     return strMonth
}

func calendar(_ calendar: JTCalendarManager!, prepareMenuItemView menuItemView: UIView!, date: Date!) 
{
     print(date.getMonthYearFromDate)

     //Do your code modification here
}