如何在DatePickerDialog中设置最小和最大日期?

时间:2013-02-26 11:49:48

标签: xamarin.android

我正在尝试将注意力设置为1900并将最大值设置为1997到DatePickerDialog。但无论我尝试什么,对话框上的结果都不是应该的。该文档说它应该是自1970年以来的毫秒数。所以这段代码应该有效。

我错过了什么?

var dialog = new DatePickerDialog(this, (ss, ee) =>
{
  var dateSelected = ee.Date;
}, model.Birthday.Year, model.Birthday.Month - 1, model.Birthday.Day);


var origin = new DateTime(1970, 1, 1);
dialog.DatePicker.MinDate = 0;// (int)(DateTime.Now.Date.AddYears(-120) - origin).TotalMilliseconds;
dialog.DatePicker.MaxDate = (int)(DateTime.Now.Date.AddYears(-8) - origin).TotalMilliseconds;

2 个答案:

答案 0 :(得分:3)

问题是TotalMilliseconds超过int可以容纳的最大值。

所以,将其投放到long会很好,实际上是DatePicker.MaxDate expects

dialog.DatePicker.MaxDate = (long)(DateTime.Now.Date.AddYears(-8) - origin).TotalMilliseconds;

答案 1 :(得分:1)

  protected override Dialog OnCreateDialog(int id)
        {
            switch (id)
            {
                case DATE_DIALOG_ID:
                    DatePickerDialog dialog = new DatePickerDialog(this, OnDateSet, date.Year, date.Month - 1, date.Day);
                    string _gs = servis.gosterilecekRandevuGunSuresi();
                    int _guns = Convert.ToInt32(_gs);



                    DateTime origin = new DateTime(1970, 1, 1);
                    string tarih = origin.Date.ToString("dd.MM.yyyy");
                    DateTime dt = Convert.ToDateTime(tarih);
                    var datetime = Convert.ToDateTime(tarih);

                    long sayi = (long)(DateTime.Now.Date - origin.Date).TotalMilliseconds;
                    long sayi2 = (long)(DateTime.Now.Date.AddDays(_guns) - origin).TotalMilliseconds;
                    dialog.DatePicker.MinDate = sayi;
                    dialog.DatePicker.MaxDate = sayi2;

                    return dialog;
            }
            return null;
        }