Skip to content
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

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

Closed
sashalondon opened this issue Apr 19, 2016 · 1 comment
Closed

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

sashalondon opened this issue Apr 19, 2016 · 1 comment

Comments

@sashalondon
Copy link

sashalondon commented Apr 19, 2016

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
}
@WenchaoD
Copy link
Owner

@sashalondon Thanks for this snippet. Directly usage of NSDateComponents is not recommended coz there might be some problems caused by time zone. Pls refer to these methods

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants