JTAppleCalendar icon indicating copy to clipboard operation
JTAppleCalendar copied to clipboard

Has anyone got this issue? DateSegmentInfo

Open aspek opened this issue 9 years ago • 5 comments

Hi all, I have tested the delegate func

func calendar(_ calendar: JTAppleCalendarView, didScrollToDateSegmentWith visibleDates: DateSegmentInfo) {
        self.setupViewsOfCalendar(from: visibleDates)
    }

func setupViewsOfCalendar(from visibleDates: DateSegmentInfo) {
  let date = visibleDates.monthDates.first!.date
 // My current month is Aug 2017
  print("Day:\(date.day)\nMonth:\(date.month)") // correct
  print("Date:\(date)") // incorrect
}

Data source is

func configureCalendar(_ calendar: JTAppleCalendarView) -> ConfigurationParameters {
        self.formatter.dateFormat = "yyyy MM dd"
        self.formatter.timeZone = Calendar.current.timeZone
        self.formatter.locale = Calendar.current.locale
        let endDate = Calendar.current.date(byAdding: .year, value: 1,to: Date())!
        let parameters = ConfigurationParameters(
            startDate: Date(),
            endDate: endDate,
            calendar :Calendar(identifier: Calendar.Identifier.gregorian)
        )
        return parameters
    }

Seems like if you try to using the "date" from DateSegmentInfo the values not correctly this log from my case

Day:1
Month:8
Date2017-07-31 17:00:00 +0000

version : 7.0

aspek avatar Aug 29 '17 04:08 aspek

Solved. But you need to convert to DateComponents instead of Date

func setupViewsOfCalendar(from visibleDates: DateSegmentInfo) {
        
        let date        = visibleDates.monthDates.first!.date
        var comDate   = DateComponents()
        comDate.day   = date.day
        comDate.month = date.month
        comDate.year  = date.year
        
        print("Day:\(date.day)\nMonth:\(date.month)\nDate\(date)")
        self.loadDataFromMonth(dateComp: comDate)
    }

Does anybody know another solution?

aspek avatar Aug 29 '17 05:08 aspek

When ever you are printing dates to the console, you have to be aware that although the date might be correct, a wrong date might be printed to the console if you are not printing it with a correct formatter.

See the answer here and let me know if this corrects your issue. -> https://github.com/patchthecode/JTAppleCalendar/issues/252

patchthecode avatar Aug 29 '17 06:08 patchthecode

Hi thanks for quick reply, My boss told me before about printing dates to console. But when I'm using date as dateComponents. I got the wrong values same as printed.

let date = visibleDates.monthDates.first!.date
print("Day:\(date.day)\nMonth:\(date.month)\nDate\(date)")

let cal = Calendar(identifier: Calendar.Identifier.gregorian)
let comDate = cal.dateComponents(in: TimeZone(secondsFromGMT: +7)!, from: date)
print("CompDay:\(comDate.day!)\nCompMonth:\(comDate.month!)\nCompDate\(comDate.date!)")
CompDay:31
CompMonth:7
CompDate2017-07-31 17:00:00 +0000

aspek avatar Aug 29 '17 06:08 aspek

let date = visibleDates.monthDates.first!.date
let formatter = DateFormatter()
formatter.timeZone = Calendar.current.timeZone
formatter.locale = Calendar.current.locale

formatter.dateFormat = "dd"
print(formatter.string(from: date))

formatter.dateFormat = "MM"
print(formatter.string(from: date))

formatter.dateFormat = "yyyy"
print(formatter.string(from: date))

works?

patchthecode avatar Aug 29 '17 06:08 patchthecode

Yes, It's should works. My intent is If use the date with directly call like
let comDate = cal.dateComponents(in: TimeZone(secondsFromGMT: +7)!, from: date) The date component will had incorrect values. these are true(actually wrong) values as printed. I have requested to my API server and It return to wrong values. Anyway Thank you for your attention.

aspek avatar Aug 29 '17 07:08 aspek