aminography / PrimeDatePicker

PrimeDatePicker is a tool that provides picking a single day, multiple days, and a range of days.
Apache License 2.0
471 stars 52 forks source link

setting Min and Max dates #56

Closed rama-2402 closed 3 years ago

rama-2402 commented 3 years ago

Hi thanks for the awesome library, you have mentioned .minPossibleDate(minDate: PrimeCalendar), .minPossibleDate(maxDate: PrimeCalendar) for setting minimum and maximum dates for selection. I was wondering if the minimum and maximum dates can be set based on Calendar.DAY_OF_MONTH.

For example, dates to be available for selection only between the range of given date.

I tried the following,


val today = CivilCalendar()

val min = CalendarFactory.newInstance(CalendarType.CIVIL)
        min.add(Calendar.DAY_OF_MONTH, -2)
val max = CalendarFactory.newInstance(CalendarType.CIVIL)
        min.add(Calendar.DAY_OF_MONTH, +2)

val datePicker = PrimeDatePicker
    .dialogWith(today)
    .pickMultipleDays(callback)
    .minPossibleDate(min)
    .maxPossibleDate(max)
    .build()

datePicker.show(activity.supportFragmentManager, "TAG")

But the calendar dates are not showing up in the dialog. Could you please provide some example code as to how i can implement this. Thanks :)

rama-2402 commented 3 years ago

BTW i've found the issue.

I've used "min" instead of "max" in,

val max = CalendarFactory.newInstance(CalendarType.CIVIL)
        min.add(Calendar.DAY_OF_MONTH, +2)

changing this fixed the issue. :)

If anyone's looking,

you can use this function to get the Int value for the min and max dates

    private fun getDate(date: Long): Int {
        var diff = (date - System.currentTimeMillis())/ (24 * 60 * 60 * 1000)
        return diff.toInt()
    }

val min = CalendarFactory.newInstance(CalendarType.CIVIL)
        min.add(Calendar.DAY_OF_MONTH, -getDate($timeInMillis))
val max = CalendarFactory.newInstance(CalendarType.CIVIL)
        max.add(Calendar.DAY_OF_MONTH, +getDate($timeInMillis))