Swift3 如何在日历视图中选择日期
您好,JTAppleClendar中的NewView。 我已经通过以下链接在JTAppleCalendar上创建了一个日历库。 在用户按下日历上的日期后,我无法计算如何获取日期 下面是代码:Swift3 如何在日历视图中选择日期,swift3,jtapplecalendar,Swift3,Jtapplecalendar,您好,JTAppleClendar中的NewView。 我已经通过以下链接在JTAppleCalendar上创建了一个日历库。 在用户按下日历上的日期后,我无法计算如何获取日期 下面是代码: 在日期中。返回日期总是迟一天??? 这就是我想要实现的。当用户点击日历上的日期时,我想获取日期并使用此日期从服务器请求数据 extension MyCalendar: JTAppleCalendarViewDataSource, JTAppleCalendarViewDelegate { fun
在日期中。返回日期总是迟一天???
这就是我想要实现的。当用户点击日历上的日期时,我想获取日期并使用此日期从服务器请求数据
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)
}
}
请帮忙
谢谢找到了答案。永远不要查看xcode控制台来查看正确的日期。print命令和XCode日期显示有自己的格式,可能与时区不同。由于格式错误,您仅在视觉上看到的日期“看起来”不正确。要在iOS中正确显示日期,请始终使用DateForMatter。我在上面发布的链接为您解释了这一点。这个问题存在于您使用的任何iOS日历库中。只是一个问题,为什么您要使用cellState.date而不是应该是您选择的日期?@FrancescoDeliro,请告诉我如何使用。我试过让d:Date=Date,但d没有。我在MilkBottle发现了这个问题。检查:你说问题是
让strdate=cellState。Date
你怎么知道这是个问题,你把它打印到控制台了吗?我是新手。我在let-strdate=cellState.date上设置了一个断点,我发现它落后了一天,所以我使用let-newDate=date(时间间隔:86400,因为strdate),这将提供正确的日期。现在我按照Francesco的说法使用let strDate=formatter.string(from:date)。它现在提供了正确的日期。请告知。谢谢