swift3jtapplecalendar

How to select Date in the CalendarView


hi newBiew in JTAppleCalendar. I have following this link for creating a Calendar base on JTAppleCalendar. https://www.youtube.com/watch?v=CQNotydm58s&index=6&list=PLpqJf39XekqyUG7dxcqGO0JNprryysv9Q

I could not figure how to get a date after user press the date on the Calendar.

Here the code:
in the didSelectDate. The date return is always one day behind??

This is what I wanted to achieve. When user tap on the date on the calendar, I want to get the date and use this date to request data from the server,

extension MyCalendar: JTAppleCalendarViewDataSource, JTAppleCalendarViewDelegate {

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

        formatter.dateFormat = "yyyy MM dd"
        formatter.timeZone = Calendar.current.timeZone
        formatter.locale = Calendar.current.locale

        let startDate = formatter.date(from: "2017 01 01")!
        let endDate = formatter.date(from: "2027 12 31")!        

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

    }


  func calendar( _ calendar: JTAppleCalendarView, cellForItemAt date: Date, cellState: CellState, indexPath: IndexPath) -> JTAppleCell{


        let cell = calendar.dequeueReusableJTAppleCell(withReuseIdentifier: "CustomCell", for: indexPath) as! CustomCell

        cell.dateLabel.text = cellState.text

        configureCell(cell:cell, cellState: cellState)

        return cell

    }


    //------------ selected item

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

        // cell is option
        configureCell(cell: cell, cellState: cellState)

       //prolem:
        let strdate = cellState.date

    }


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

        // cell is option
        configureCell(cell: cell, cellState: cellState)

    }

   func calendar(_ calendar: JTAppleCalendarView, didScrollToDateSegmentWith visibleDates: DateSegmentInfo) {

       setupCalendarView(dateSegment: visibleDates)

    }

}

Please help,

Thanks


Solution

  • the answer is found in this github issue. Never look at xcode console to see the correct date. The print command and also the XCode display for dates comes with its own formatting with might be different than your time zone. The date you see visually only "looks" incorrect because of that wrong formatting. To display dates correctly in iOS, always use a DateForamatter. The link i posted above explains it for you. This question exists in any iOS calendar library you use.