-
Notifications
You must be signed in to change notification settings - Fork 801
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
Wrong selected date #252
Comments
By the way, you are using a lot of defaults. func configureCalendar(_ calendar: JTAppleCalendarView) -> ConfigurationParameters {
return ConfigurationParameters(startDate: startDate!, endDate: endDate!)
} Let me know if this has resolved your issue. |
Thank you for your quick reply. I will be able to test in 12 hours and will post if it is ok or not. |
Updated to the 6.1.1 and selected date is still wrong... My timeZone is GMT+2. |
Version: 5.0.1 has the same problem ,because we need use swift 2.3 ,so can not update to the latest |
ok. @onurays Can I see the following codes?
I need this information so that I know how your date is showing up. |
@nlucky let me resolve this on version 6 first. Then i'll port the fix to you. |
@patchthecode thanks |
I am also experiencing this issue. 1. Configure Calendar Code func configureCalendar(_ calendar: JTAppleCalendarView) -> ConfigurationParameters {
let startDate = Date()
let endDate = Date() + 1.year
let parameters = ConfigurationParameters(
startDate: startDate,
endDate: endDate,
numberOfRows: 1,
calendar: Calendar.current,
generateInDates: .forAllMonths,
generateOutDates: .off,
firstDayOfWeek: .monday
)
return parameters
} 2. Code to print the date on selection func calendar(_ calendar: JTAppleCalendarView, didSelectDate date: Date, cell: JTAppleDayCellView?, cellState: CellState) {
handleCellSelection(view: cell, cellState: cellState)
handleCellTextColor(view: cell, cellState: cellState)
self.habitsViewModel.selectedDate.value = date
print("\(date)")
} LLDB Output for po Calendar.current:
|
@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: func calendar(_ calendar: JTAppleCalendarView, didSelectDate date: Date, cell: JTAppleDayCellView?, cellState: CellState) {
handleCellSelection(view: cell, cellState: cellState)
handleCellTextColor(view: cell, cellState: cellState)
print(date)
print(formatter.string(from: date))
} And the results after selecting the
It is important to understand that when you do a In order to print the correct date on console, you need a formatter that is configured with the same you can do this by:
This will give you the same date formatted to your Lesson to be learned: printing dates directly on console using the I hope this solves your issue? |
@onurays if this also resolves your issue, then feel free to leave a Star Github rating :) |
@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, By the way, you already had your star for this beautiful library :) |
@onurays but thats the thing. The For instance, put this in your PlayGround: import UIKit
// ____________________________________________________________________________
// First I create a calendar to match your Calendar.current in Berlin
var myCalendar1 = Calendar(identifier: .gregorian)
myCalendar1.timeZone = TimeZone(identifier: "Europe/Berlin")!
myCalendar1.locale = Locale(identifier: "en")
// Next I create my date formatter
let aFormatter1 = DateFormatter()
aFormatter1.dateFormat = "yyyy MM dd"
aFormatter1.timeZone = myCalendar1.timeZone
aFormatter1.locale = myCalendar1.locale
// Even though theFifth is the 5th, Myplayground is showing it as the 4th. (on the right)
// But date actually IS the fifth. You can operate on it without converting it to strings
let theFifth1 = aFormatter1.date(from: "2017 01 05")
// ____________________________________________________________________________
// This second calendar in my own time zone
var myCalendar2 = Calendar(identifier: .gregorian)
myCalendar2.timeZone = TimeZone(identifier: "America/Vancouver")!
myCalendar2.locale = Locale(identifier: "en_US")
// This is a second formatter in my own time zone
let aFormatter2 = DateFormatter()
aFormatter2.dateFormat = "yyyy MM dd"
aFormatter2.timeZone = myCalendar2.timeZone
aFormatter2.locale = myCalendar2.locale
let theFifth2 = aFormatter2.date(from: "2017 01 05") 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. |
Yes you have a point. For me it is important to work with the "local date".
//date is the object coming from the didSelectDate delegate method
let dateInRegion = date.inLocalRegion()
let seconds = dateInRegion.region.timeZone.secondsFromGMT(for: date)
let localDate = Date(timeInterval: TimeInterval(seconds), since: date) 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 you are exactly correct. I made this library to work with SwiftDate because a number of users requested it. So you can either do the conversion yourself, or you can simply use SwiftDate. |
@patchthecode All right then, thank you for your afford to reproduce the issue. I also see the same issue with other libraries. So (as an Android developer), this is not a problem for me, anymore. I will format the date and not look at the console :) Thanks again. |
@onurays haha. Yea, when it comes to dates, console can really trip you up if youre not formatting it. Cheers. |
I fixed this problem by modifying the time zone in my device calendar like this : then in JTAppleCalendar ConfigurationParameters returned start date, end date and the previous calendar: |
You can get current Date by using |
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 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? |
Version: 6.0
Selected Date: (Date) $R0 = 2017-01-09 22:00:00 UTC
Here is my configuration and the problem occurs even if I don't set a timezone.
The text was updated successfully, but these errors were encountered: