3

How can I get current calendar start and end date?

From the screenshot, I need to get Sep 24 2017 and Nov 4 2017..

thanks![enter image description here]1

dr.calix
  • 667
  • 8
  • 21

2 Answers2

3
var dateArray = [Date]()
for cell:FSCalendarCell in calendar.visibleCells() {
  if cell.isPlaceholder {
    dateArray.append(calendar.date(for: cell)!)
  }
}

calendar.select(dateArray.min())
calendar.select(dateArray.max())
Neethu M
  • 644
  • 6
  • 11
  • I tried doing your suggestion but I got Start: `Optional(2017-08-26 16:00:00 +0000)` and End: `Optional(2017-10-03 16:00:00 +0000)` – dr.calix Jul 25 '17 at 18:32
  • Yes, that's how it would get printed. Please try selecting the dates in the calendar. Check my updated answer. – Neethu M Jul 26 '17 at 04:42
3

Here is how you can see the first and last visible date

let startDate: Date?
let endDate: Date?
if self.calendar.scope == .week {
    startDate = self.calendar.currentPage
    endDate = self.calendar.gregorian.date(byAdding: .day, value: 6, to: startDate)
} else { // .month
    let indexPath = self.calendar.calculator.indexPath(for: self.calendar.currentPage, scope: .month)
    startDate = self.calendar.calculator.monthHead(forSection: (indexPath?.section)!)!
    endDate = self.calendar.gregorian.date(byAdding: .day, value: 41, to: startDate)
}

The answer is originally mentioned in this thread

Gulfam Khan
  • 1,010
  • 12
  • 23