Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

headerString(_ date: Date) offsets month by -1 #109

Open
zeeshanz opened this issue Jan 29, 2020 · 4 comments
Open

headerString(_ date: Date) offsets month by -1 #109

zeeshanz opened this issue Jan 29, 2020 · 4 comments

Comments

@zeeshanz
Copy link

zeeshanz commented Jan 29, 2020

Calls to self.calendarView.goToPreviousMonth(), self.calendarView.goToNextMonth() and self.calendarView.setDisplayDate(Date(), animated: true) calls protocol func headerString(_ date: Date) -> String? twice and offsets the month by -1 every time, meaning the month is offset twice to -2.

The sample code you provide is using an older version of KDCalendar which works fine and doesn't call headerString(). But newer version of KDCalendar requires to call it.

func headerString(_ date: Date) -> String? {
    print("Selected Date is: \(date.onlyMonth)")
    return date.onlyMonth // onlyMonth is I extension which grabs the month portion of the date
}

This is January. I call goToPreviousMonth() and it changes the header to February, but then immediately jumps back to January while displaying days of February.

Similarly, when I sent it current Date(), it sets header to December while showing days of January.

wrong_month
wrong_month3
wrong_month2

@tys-adventure
Copy link

@zeeshanz did you find a fix for this? I'm dealing with a similar issue.

@zeeshanz
Copy link
Author

zeeshanz commented May 29, 2020

@TyJPhillips Here is how I am doing it. The 'fix' is the function setMonthButtonsTitle()

    private var selectedDate = Date()
    private let MONTHS = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]

    @IBAction func goToPreviousMonthAction(_ sender: Any) {
        selectedDate = Calendar.current.date(byAdding: .month, value: -1, to: selectedDate)!
        calendarView.goToPreviousMonth()
        setMonthButtonsTitle()
    }
    
    @IBAction func goToNextMonthAction(_ sender: Any) {
        selectedDate = Calendar.current.date(byAdding: .month, value: 1, to: selectedDate)!
        calendarView.goToNextMonth()
        setMonthButtonsTitle()
    }
    
    @IBAction func goToCurrentMonthAction(_ sender: Any) {
        selectedDate = Date()
        calendarView.setDisplayDate(Date(), animated: true)
        setMonthButtonsTitle()
    }

    private func setMonthButtonsTitle() {
        let month = selectedDate.onlyMonth - 1
        let prevMonth = (month == 0 ? 11 : month - 1)
        let nextMonth = (month == 11 ? 1 : month + 1)
        currentMonthButton.setTitle(MONTHS[Date().onlyMonth - 1], for: .normal)
        nextMonthButton.setTitle(MONTHS[nextMonth], for: .normal)
        previousMonthButton.setTitle(MONTHS[prevMonth], for: .normal)
    }

And the onlyMonth code is in an extension to the Date like this:

extension Date {
        var onlyMonth: Int {
        let formatter = DateFormatter(); formatter.dateFormat = "MM"
        return Int(formatter.string(from: self as Date))!
    }
}

@zeeshanz zeeshanz reopened this May 29, 2020
@tys-adventure
Copy link

@zeeshanz Thank you so much! I'll see if this helps with my issue!

@salmankhalid876
Copy link

Any update?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants