Jtapplecalendar: Printing incorrect Date in method didSelectDate

Created on 18 Feb 2017  路  3Comments  路  Source: patchthecode/JTAppleCalendar

When I try to print the user selected date within the below function, I get a date 1 day lesser than the selected date. This happens even in the calendar tutorial examples

Please suggest, How I could fix this issue ?

func calendar(_ calendar: JTAppleCalendarView, didSelectDate date: Date, cell: JTAppleDayCellView?, cellState: CellState) {
        handleCellSelection(view: cell, cellState: cellState)
        handleCellTextColor(view: cell, cellState: cellState)
        selectedDate.text = String(describing: date)
        print("Date \(date)")
    }

incorrectdate

This is not a bug. Works as expected.

Most helpful comment

Date is correct.
You just need to format it.
Let me know if the following helps you --> https://github.com/patchthecode/JTAppleCalendar/issues/252#issuecomment-270436780

When dealing with Dates, printing it to the console will give you incorrect results depending on your timeZone. Never simply print a date. The correct way to handle dates is to use DateFormatters if you want the correct date printed to console.

All 3 comments

Date is correct.
You just need to format it.
Let me know if the following helps you --> https://github.com/patchthecode/JTAppleCalendar/issues/252#issuecomment-270436780

When dealing with Dates, printing it to the console will give you incorrect results depending on your timeZone. Never simply print a date. The correct way to handle dates is to use DateFormatters if you want the correct date printed to console.

If you still need help on this, I'll be available in the morning.

Your fix is simple. Create a DateFormatter, then set its timeZone and locale to be the same as the calendar() instance you supplied in the configureCalendar function. Then simply print the stringFromDate

Thanks again, I got it working by using the below code:

let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZZ"
print("printing current Date\(formatter.string(from: date))")
Was this page helpful?
0 / 5 - 0 ratings

Related issues

Kevinw14 picture Kevinw14  路  3Comments

programus picture programus  路  5Comments

blinkmeoff picture blinkmeoff  路  3Comments

leo150 picture leo150  路  3Comments

VladFrolov picture VladFrolov  路  4Comments