Jtapplecalendar: didSelectDate-Method delivers wrong selected day

Created on 9 Apr 2018  路  8Comments  路  Source: patchthecode/JTAppleCalendar

Hi,

first many thanks for the great calendar. Find it couple days ago, and till now im able to configure all needed customizations. But I have a weird behavior that I do not understand, which also sounds buggy. Im using the latest version i.e. v7.1.5.

Im configuring the calendar from first of current month till last day of current year:

extension ViewController: JTAppleCalendarViewDataSource {
    func configureCalendar(_ calendar: JTAppleCalendarView) -> ConfigurationParameters {
        formatter.dateFormat = "yyyy MM dd"
        formatter.timeZone = Calendar.current.timeZone
        formatter.locale = Calendar.current.locale

        let startDate = Calendar.current.dateInterval(of: .month, for: Date())?.start
        let endYear = Calendar.current.component(.year, from: Date())
        let endDate = formatter.date(from: "\(endYear) 12 31")

        let parameters = ConfigurationParameters(startDate: startDate!, endDate: endDate!)
        return parameters
    }
}

In didSelectDate-Method i just printed out the current date of selected cell. What i get, is the day before the day in selected cell.

func calendar(_ calendar: JTAppleCalendarView, didSelectDate date: Date, cell: JTAppleCell?, cellState: CellState) 
{

        print("date: \(date)")

        print("calendar.selectedDate: \(calendar.selectedDates)")

        handleDateRange(selectedDate: date)

        handleCellAppearance(cell: cell, cellState: cellState)
}

For example:

  • I click on 10.04.2018 and i get the 09.04.2018 in "didSelectDate-Method"
  • I click on 01.04.2018 and i get the 31.03.2018

Can you please clarify.

Many thanks and nice day,
MG

This is not a bug. Works as expected.

All 8 comments

This is expected and has been asked very many times :)
This happens to users who live in the east.

And it happens to any calendar lib you use.
Here is why it happens, and how to fix it -> https://github.com/patchthecode/JTAppleCalendar/issues/252

Let em know if it is resolved.

Somehow I did not found related issues. Thanks for the info. I expected that it has something to do with the formatter. I configured my calendar with my custom formatter, that is why I expected this to be enough for the calendar to handle it correctly.

I will check this now, yet it is not just about printing the date. I make at some stages in code some date comparison, where it also fails, due to this behavior. For example, Im checking if selectedDate is smaller than Today i.e.
if date < Date()

Due to this behavior the today is also included to, which should not!
I'll check and give feedback. Thank you.

Im not using SwiftDate, and need to handle this properly. Is there a best practice for this?

You do not need to use SwiftDate.
You just need a DateFormatter() as shown in the issue.

    func configureCalendar(_ calendar: JTAppleCalendarView) -> ConfigurationParameters {

        let testCalendar = Calendar(.........) // Configure a calendar for your time zone here
        let formatter = DateFormatter()
        formatter.dateFormat = "yyyy MM dd"
        formatter.timeZone = testCalendar.timeZone
        formatter.locale = testCalendar.locale

        let startDate = formatter.date(from: "2018 01 01")!
        let endDate = formatter.date(from: "2019 02 01")!

        let parameters = ConfigurationParameters(
                                                 startDate: startDate,
                                                 endDate: endDate,
                                                 calendar: testCalendar) // Pass your calendar here
        return parameters
    }

Now whenever you need to print dates etc, use your same formatter

let formatter = DateFormatter()

Your date comparisons should also work

This is working, as i used GMT as an identifier. But for someone in the far east or in the west, this will not work properly. Im working for the first time with Calendars. How would you handle this?

By the way, did you ever considered making your Calendar-Lib reactive conform? Binding data for UICollectionView is supported in Libs such as Reactivekit/Bond. This makes the binding and updating data as well as maintaining the code much better..

How would you handle this?

Well this would be an issue out of the scope of this calendar. The question you are asking now is how do iOS apps handle dates across different regions globally. You can take the harder route and handle it manually, or use other cocoapods like SwiftDate which was developed specifically because of this problem :)

did you ever considered making your Calendar-Lib reactive conform?

I have never heard about this before. I will take a look to see how to make this lib conform.

Many thanks for the support.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

zhanswift picture zhanswift  路  5Comments

blinkmeoff picture blinkmeoff  路  3Comments

dbmrq picture dbmrq  路  5Comments

programus picture programus  路  3Comments

VladFrolov picture VladFrolov  路  4Comments