DatePickerDialog showing with month forward than what is set in its constructor(Xamarin.Android)

352 Views Asked by At

I declare the following variables:

private int year = DateTime.Now.Year, month = DateTime.Now.Month, day = DateTime.Now.Day;

I create DatePickerDialog:

DatePickerDialog datePickerDialog = new DatePickerDialog(this, this, year, month, day);

When I call its method show:

datePickerDialog.Show();

It displays a calendar with 09.03.2018 selected when it has to be the date today 09.02.2018. Then when I choose some date from the displayed calendar it chooses a date with one month backward from what I've choosen for example if I choose 10.3.2018 It says me that I've selected 10.02.2018 Here is all my activity:

public class OrdersActivity : Activity, IOnDateSetListener
{
    private int year = DateTime.Now.Year, month = DateTime.Now.Month, day = DateTime.Now.Day;
    Button btnDatePicker;

    //set btnDatePicker's text to be whatever user has selected
    public void OnDateSet(DatePicker view, int year, int month, int dayOfMonth)
    {
        this.year = year;
        this.month = month;
        this.day = dayOfMonth;
        string selectedDate = String.Format(day + "." + month + "." + year);
        btnDatePicker.Text = selectedDate;
    }

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        SetContentView(Resource.Layout.Orders);

        DatePickerDialog datePickerDialog = new DatePickerDialog(this, this, year, month, day);

        //set btnDatePicker's text to be DateTime.Now
        btnDatePicker = FindViewById<Button>(Resource.Id.btnDatePicker);
        btnDatePicker.Text = DateTime.Now.ToString("dd.MM.yyyy");

        btnDatePicker.Click += delegate {
            datePickerDialog.Show();
        };

}
1

There are 1 best solutions below

2
Robbit On BEST ANSWER

It displays a calendar with 09.03.2018 selected when it has to be the date today 09.02.2018

Please check your phone's time, it works on my phone.

if I choose 10.3.2018 It says me that I've selected 10.02.2018

Yes, in the OnDateSet method, the month starts at 0, with 0 for January and 1 for February. You need use this.month=month+1;, and replace

string selectedDate = String.Format(day + "." + month + "." + year);

with

string selectedDate = String.Format(day + "." + this.month + "." + year);


Update:

As I have said, the month starts at 0, so you need -1 and +1, look at the below codes based on your codes.

public class MainActivity : Activity, IOnDateSetListener
{
    private int year = DateTime.Now.Year, month = DateTime.Now.Month-1, day = DateTime.Now.Day;
    Button btnDatePicker;

    //set btnDatePicker's text to be whatever user has selected 
    public void OnDateSet(DatePicker view, int year, int month, int dayOfMonth)
    {
        this.year = year;
        this.month = month+1;
        this.day = dayOfMonth;
        string selectedDate = String.Format(day + "." + this.month + "." + year);
        btnDatePicker.Text = selectedDate;
    }

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        SetContentView(Resource.Layout.Main);

        DatePickerDialog datePickerDialog = new DatePickerDialog(this, this, year, month, day);

        //set btnDatePicker's text to be DateTime.Now 
        btnDatePicker = FindViewById<Button>(Resource.Id.btnDatePicker);
        btnDatePicker.Text = DateTime.Now.ToString("dd.MM.yyyy");

        btnDatePicker.Click += delegate
        {
            datePickerDialog.Show();
        };
    }
}