Skip to content

Don't allow date in the future to be selected. #273

@sashalondon

Description

@sashalondon

Hi I wanted to share this with you as I thought it might be useful to someone. Its not an issue and don't think its big enough to for a pull request.

With this in swift you can make calendar only selectable in the past.

func calendar(calendar: FSCalendar, shouldSelectDate date: NSDate) -> Bool {
// dont allow date in future to be selected.

let todayDate = NSDate()
let calendar = NSCalendar.self.currentCalendar()

let components = calendar.components([.Day , .Month , .Year, .Hour, .Minute], fromDate: todayDate)
let year =  (components.year)
let month = (components.month)
let day = (components.day)

let todayFigure = (year * 10000) + (month * 100) + day

let selectedComponents = calendar.components([.Day , .Month , .Year, .Hour, .Minute], fromDate: date)
let selectedYear =  (selectedComponents.year)
let selectedMonth = (selectedComponents.month)
let selectedDay = (selectedComponents.day)

let selectedFigure = (selectedYear * 10000) + (selectedMonth * 100) + selectedDay

if selectedFigure > todayFigure {
  return false
}
return true
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions