weilsonwonder / WWCalendarTimeSelector

A android inspired iOS date/time picker
MIT License
317 stars 126 forks source link

disable past date and a range of time. #28

Closed RnyG closed 4 years ago

RnyG commented 7 years ago

I think this library is awesome, i'm using the swift 2.3 version (i think so) But i don't figure out how use "WWCalendarTimeSelectorShouldSelectDate(selector: WWCalendarTimeSelector, date: NSDate) -> Bool" to disable past dates, i need to set user able to select dates next 3 days from today or select any date before 1998, and in clock view, select hour in 8am- 8pm. How i can do that? Thanks!

tanand7 commented 7 years ago

I am also facing same issue. What is the use of setStartDate and setEndDate? Are they used to disable the old dates?

RnyG commented 7 years ago

@tanand7 hi, setStartDate and setEndDate are used in cases that user select multiples dates, but if you want disable past dates you can use this code:

func WWCalendarTimeSelectorShouldSelectDate(selector: WWCalendarTimeSelector, date: NSDate) -> Bool { if date.timeIntervalSinceNow.isSignMinus { //myDate is earlier than Now (date and time) return false } else { //myDate is equal or after than Now (date and time) return true } }

you also can change timeIntervalSinceNow for timeIntervalSinceDate(yourSpecificDate) and it will work perfectly.

If you want to disable future dates like was my case too you can use this tricky code (also you can do better haha)

var dateYearsAgo = NSDate() var oneDayMore = NSDate() let calendar = NSCalendar.currentCalendar() //When you call selector you do this. dateYearsAgo = calendar.dateByAddingUnit(.Year, value: -18, toDate: NSDate(), options: [])! let components = calendar.components([.Year, .Month, .Day], fromDate: NSDate()) components.day = dateYearsAgo.day + 1 components.month = dateYearsAgo.month components.year = dateYearsAgo.year oneMore = calendar.dateFromComponents(components)! selector.optionCurrentDate = dateYearsAgo

//Then you do func WWCalendarTimeSelectorShouldSelectDate(selector: WWCalendarTimeSelector, date: NSDate) -> Bool { if date.timeIntervalSinceDate(next3days).isSignMinus { //myDate is earlier than Now (date and time) return true } else if date.timeIntervalSinceDate(next3days) < oneMore.timeIntervalSinceDate(next3days){ //myDate is equal or after than Now (date and time) return true }else{ return false } }

sunil1790 commented 7 years ago

I was able to disable dates(custom date) but changing color of disabled date is still a mystery for me any idea how we can change color of disabled date(till any custom date) I want to do it the way it is done for ### selector.optionCalendarFontColorPastDates = UIColor.lightGray

sterien7 commented 6 years ago

@tanand7 do we have any update on the color of disabled days? I am able to disallow user from selecting them but I want also the color to be different

amrangry commented 6 years ago

it seems there is no update regards this issue "Color for disabled dates"

Ali-Sher commented 6 years ago

How to change color and Disable of past time i.e Hour and minutes.. any help.

sarath435 commented 5 years ago

if date.timeIntervalSinceNow.isSignMinus { //myDate is earlier than Now (date and time) return false } else { //myDate is equal or after than Now (date and time) return true } //NOTE getting error in issignminus...... help me thanks in advance

fukemy commented 4 years ago

if date.timeIntervalSinceNow.isSignMinus { //myDate is earlier than Now (date and time) return false } else { //myDate is equal or after than Now (date and time) return true } //NOTE getting error in issignminus...... help me thanks in advance

Hi, can you give me the position to put this code? thanks

ghost commented 4 years ago

@fukemy I think inside WWCalendarTimeSelectorShouldSelectDate protocol