JTAppleCalendar: Wrong selected date
Version: 6.0
Selected Date: (Date) $R0 = 2017-01-09 22:00:00 UTC
func calendar(_ calendar: JTAppleCalendarView, didSelectDate date: Date, cell: JTAppleDayCellView?, cellState: CellState) {
handleCellTextColor(view: cell, cellState: cellState)
handleCellSelection(view: cell, cellState: cellState, date: date, isSelectionEvent: true)
}
Here is my configuration and the problem occurs even if I don’t set a timezone.
func configureCalendar(_ calendar: JTAppleCalendarView) -> ConfigurationParameters {
var calendar = Calendar.current
calendar.timeZone = TimeZone(secondsFromGMT: NSTimeZone.local.secondsFromGMT())!
return ConfigurationParameters(startDate: startDate!, endDate: endDate!, numberOfRows: 6, calendar: calendar, generateInDates: .forAllMonths, generateOutDates: .tillEndOfGrid, firstDayOfWeek: .monday)
}
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 19 (8 by maintainers)
Yes you have a point. For me it is important to work with the “local date”. So for me this is resolved likes this:
If i now print localDate I get 2017-01-04 00:00:00 +0000 which is the correct date for me as my app proceeds. The above code snippet makes use of the superb SwiftDate library by the way which makes working with dates a lot easier and more convenient. I am not a contributor or anything for the library I just thought I would mention it if people would try to go with my solution and wonder why it does not work for them.
@dehlen I see the issue. I do not believe there is an error. Here are my results.
I made my time zone information to be the same as yours:
And i had this code:
And the results after selecting the
10th
were:It is important to understand that when you do a
print(date)
what is happening here is that the print command tries to format that date with its own formatting. This formatting may be different than your timeZone and locale.In order to print the correct date on console, you need a formatter that is configured with the same
timeZone
andlocale
information of yourCalendar()
instance.you can do this by:
This will give you the same date formatted to your
time zone
rather than what ever default time zone contained in theprint
command.Lesson to be learned: printing dates directly on console using the
print
command can be very deceptive.I hope this solves your issue?
This may be a dumb/blunt solution but maybe it takes out some of the hassle:
Could those errors be resolved by simply setting the time in the
selectedDate
property to12:00 UTC
instead of00:00 UTC
?Then the selected day should be the same across the globe and since we’re dealing with calendar dates, the timestamp shouldn’t be relevant.
Or am I thinking the wrong way?
@onurays but thats the thing. The
date
is correct. You can do operations on dates and it should work properly. Its only when you use aprint
command, you have to format it so that it displays correctly on the console.For instance, put this in your PlayGround:
Your results should look like this:
Although they are the same date, they are displayed differently on console. But you do not have to change the dates into string to work with them. You can use them directly.
Your calendar might look perfect with FSCalendar in your timeZone right now, but are you sure it will still be displayed the same way in other regions around the world?
Here are some issues on FSCalendar that has the same problems you are experiencing:
In all responses, the answer is the same. Format your date using the correct formatter. When ever we work with dates, we should always take regions into consideration. Therefore, whether you use FSCalendar, or this library, make sure you format correctly. Cheers 🍻
@patchthecode Unfortunately, I had to switch to the FSCalendar (because of the time pressure). I will try to test it again at weekend.
I know,
DateFormatter
formats correctly. Butpo
orprint(date)
doesn’t. So we have problems if we need operations on Dates, not Strings.By the way, you already had your star for this beautiful library 😃