4

I'm using the xamarin android datepicker dialog fragment. but tried to enable date between today and toady + 3 days. It's not working. It's not even working for min date options only.

public static readonly string TAG = "X:" + typeof (DatePickerFragment).Name.ToUpper();
        Action<DateTime> _dateSelectedHandler = delegate { };

        public void OnDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
        {
            // Note: monthOfYear is a value between 0 and 11, not 1 and 12!
            DateTime selectedDate = new DateTime(year, monthOfYear +1, dayOfMonth);
            Log.Debug(TAG, selectedDate.ToLongDateString());
            _dateSelectedHandler(selectedDate);
        }

        public static DatePickerFragment NewInstance(Action<DateTime> onDateSelected)
        {
            DatePickerFragment frag = new DatePickerFragment();
            frag._dateSelectedHandler = onDateSelected;
            return frag;
        }

        public override Dialog OnCreateDialog(Bundle savedInstanceState)
        {
            DateTime currently = DateTime.Now;
            DatePickerDialog dialog = new DatePickerDialog(Activity, this, currently.Year, currently.Month - 1,
                                                           currently.Day);

            dialog.DatePicker.MinDate = currently.Millisecond;
            dialog.DatePicker.MinDate = currently.AddDays(3).Millisecond;

            return dialog;
        }
ankuranurag2
  • 2,300
  • 15
  • 30
Balaji Marimuthu
  • 1,940
  • 13
  • 13

2 Answers2

6

You could change the code of OnCreateDialog.

 public override Dialog OnCreateDialog(Bundle savedInstanceState)
    {
        DateTime currently = DateTime.Now;
        DatePickerDialog dialog = new DatePickerDialog(Activity,
                                                       this,
                                                       currently.Year,
                                                       currently.Month - 1,
                                                       currently.Day);

       dialog.DatePicker.MinDate = (long)(DateTime.Now.Date - new DateTime(1970, 1, 1)).TotalMilliseconds-1000 * 60 * 60 * 24 * 3;
       dialog.DatePicker.MaxDate = (long)(DateTime.Now.Date - new DateTime(1970, 1, 1)).TotalMilliseconds + 1000 * 60 * 60 * 24 * 3;
        return dialog;
    }

There is screenshot of DatePicker.Note: i set the minimum date is three days ago, max date is three days later.

enter image description here

Leon
  • 8,404
  • 2
  • 9
  • 52
  • 1
    Can you explain why the subtract of DateTime.Now.Date - new DateTime(1970, 1, 1) required? and any idea about how to enable different ranges? for ex: today + 3days and (today+7day)+3days – Balaji Marimuthu Jan 25 '19 at 03:33
  • Firs 60 are the seconds , next 60 are minutes , 24 are hours and 3 are days. For example: 60*60*24 = 1 day ; 60*60*24*7= 7day – DespeiL May 10 '19 at 10:12
  • And DateTime(1970, 1, 1) is start of UNIX time count – DespeiL May 10 '19 at 10:20
3

Just a small improvement for previous answer for developers who don't want to count ranges by hands.

public override Dialog OnCreateDialog(Bundle savedInstanceState)
{
    DateTime currently = DateTime.Now;
    DatePickerDialog dialog = new DatePickerDialog(Activity,
                                                   this,
                                                   currently.Year,
                                                   currently.Month - 1,
                                                   currently.Day);

    dialog.DatePicker.MinDate = (long)(currently.AddDays(-3) - new DateTime(1970, 1, 1)).TotalMilliseconds;
    dialog.DatePicker.MaxDate = (long)(currently.AddDays(3)  - new DateTime(1970, 1, 1)).TotalMilliseconds;
    return dialog;
}
NightMan
  • 31
  • 2