1

I would like to make the calendar show particular month and I would like to define range of allowed dates in calendar

DateTime today = DateTime.Today;
DatePickerDialog dateDialog = new DatePickerDialog(this, this.OnToDateSet, today.Year, today.Month - 1, today.Day);
dateDialog.DatePicker.MaxDate = DateTime.Today.Millisecond;
dateDialog.DatePicker.MinDate = new DateTime(today.Year, today.Month - 2, today.Day).Millisecond;
dateDialog.Show();  

this is what I get in return ... it shows the wrong year & month when it appears

if I comment out maxdate and mindate then calendar opens at right year and month

enter image description here

Somebody please clarify

Elstine P
  • 340
  • 4
  • 22
  • Possible duplicate of https://stackoverflow.com/questions/16749361/how-set-maximum-date-in-datepicker-dialog-in-android –  Aug 29 '17 at 20:05
  • Possible duplicate of [How set maximum date in datepicker dialog in android?](https://stackoverflow.com/questions/16749361/how-set-maximum-date-in-datepicker-dialog-in-android) – Rufus L Aug 29 '17 at 20:10
  • I'm not sure about this, but with other frameworks I had to write a check after the date was picked, and if it was outside of the bounds I wouldn't let them save it. – Ryan Ternier Aug 29 '17 at 20:16

1 Answers1

1

this is what I get in return ... it shows the wrong year & month when it appears

if I comment out maxdate and mindate then calendar opens at right year and month

If you debug your codes, you will find DateTime.Today.Millisecond and new DateTime(today.Year, today.Month - 2, today.Day).Millisecond returns 0. This is where things went wrong. In Xamarin, if you want to get the Millisecond, you need to do a DateTime offset:

DateTime today = DateTime.Today;
DatePickerDialog dateDialog = new DatePickerDialog(this, this, today.Year, today.Month - 1, today.Day);
//DateTime.MinValue isn't 1970/01/01 so we need to create a min date manually
double maxSeconds = (DateTime.Today - new DateTime(1970, 1, 1)).TotalMilliseconds;
double minSeconds = (new DateTime(today.Year, today.Month - 2, today.Day) - new DateTime(1970, 1, 1)).TotalMilliseconds;
dateDialog.DatePicker.MaxDate = (long)maxSeconds;
dateDialog.DatePicker.MinDate = (long)minSeconds;
dateDialog.Show();
Community
  • 1
  • 1
Elvis Xia - MSFT
  • 10,801
  • 1
  • 13
  • 24
  • Thanks for the answer ! Can you please explain me .. .about the DateTimeOffiset please ? Its confusing to me – Elstine P Aug 30 '17 at 09:59
  • inorder to make calendar show the current month ... I have to subtract a month .. which is not right ? please see `DatePickerDialog dateDialog = new DatePickerDialog(this, this.OnToDateSet, today.Year, today.Month - 1, today.Day);` – Elstine P Aug 30 '17 at 10:00
  • reducing one month to get the calendar to show current month doesn't make sense to me...does the month start from 0 or something – Elstine P Aug 30 '17 at 13:29