Xamarin forms: Get the next and previous dates with a selected date.

SreeeeSreeee INMember ✭✭✭✭✭

I am using the following codes for getting the next and previous day details with a selected day. I have 2 buttons named next and previous for getting the next previous dates.

//Saving the current date
string selectedDate = DateTime.Now.ToString("dd-MM-yyyy");

//Previous day
public void PrevButtonClicked(object sender, EventArgs args)
{
   DateTimeOffset dtOffset;
   if (DateTimeOffset.TryParse(selectedDate, null, DateTimeStyles.None, out dtOffset))
   {
      DateTime myDate = dtOffset.DateTime;
      selectedDate = myDate.AddDays(-1).ToString("dd-MM-yyyy");
   }
}
//Next day
public void NextButtonClicked(object sender, EventArgs args)
{
    DateTimeOffset dtOffset;
    if (DateTimeOffset.TryParse(selectedDate, null, DateTimeStyles.None, out dtOffset))
    {
       DateTime myDate = dtOffset.DateTime;
       selectedDate = myDate.AddDays(+1).ToString("dd-MM-yyyy");
     }
 }

If I click the previous button I get 03-04-2019 as the result. If again pressed the previous button I get 02-10-2019. Same for the next buttons. Based on the selected date, it will return the next or previous date.

This feature working perfectly in android and windows. But in ios getting the wrong result with this code. Is this the correct way of achieving this feature?

Best Answers

  • SreeeeSreeee IN ✭✭✭✭✭
    edited October 2019 Accepted Answer

    @LeonLu Solved :)

    public partial class MainPage : ContentPage
    {
      int year, month, day;
    
      public MainPage()
      {
         InitializeComponent();
    
         dateLabel.Text = DateTime.Now.ToString("dd-MM-yyyy");
    
         year = DateTime.Now.Year;
         month = DateTime.Now.Month;
         day= DateTime.Now.Day;
      }
    
      private void Button_Clicked(object sender, EventArgs e)
      {
        DateTime nowDate = new DateTime(year, month, day);
    
        var previewDate = nowDate.AddDays(-1);
    
        dateLabel.Text = previewDate.ToString("dd-MM-yyyy");
    
        year = previewDate.Year;
        month = previewDate.Month;
        day = previewDate.Day;
      }
    
      private void Button_Clicked_1(object sender, EventArgs e)
      {
         DateTime nowDate = new DateTime(year, month, day);
    
         var nextDate = nowDate.AddDays(+1);
    
         dateLabel.Text = nextDate.ToString("dd-MM-yyyy");
    
         year = nextDate.Year;
         month = nextDate.Month;
         day = nextDate.Day;
      }
    }
    

    enter image description here

Answers

  • LeonLuLeonLu Member, Xamarin Team Xamurai

    @Sreeee

    Please change the method like following format.

         public void PrevButtonClicked(object sender, EventArgs args)
        {
          //  DateTimeStyles;
            DateTimeOffset dtOffset;
            if (DateTimeOffset.TryParse(selectedDate, null, DateTimeStyles.None, out dtOffset))
            {
              //  DateTime myDate = dtOffset.DateTime;
    
                selectedDate = DateTime.Now.AddDays(-1).ToString("dd-MM-yyyy");
            }
        }
        //Next day
        public void NextButtonClicked(object sender, EventArgs args)
        {
            DateTimeOffset dtOffset;
            if (DateTimeOffset.TryParse(selectedDate, null, DateTimeStyles.None, out dtOffset))
            {
              //  DateTime myDate = dtOffset.DateTime;
                selectedDate = DateTime.Now.AddDays(+1).ToString("dd-MM-yyyy");
            }
        }
    
  • SreeeeSreeee INMember ✭✭✭✭✭

    @LeonLu This is not what I am expecting.

    If I click the previous button once, I need 03-10-2019 as output(since the current day is 04-10-2019). If I click again on the previous button I need 02-10-2019 as output, similarly for the next tap 01-10-2019 as output. Same for the next button, if I click the next button I need the next day based on the current day. If now I am on October 1, I need 02-10-2019 as output. Hope you understand.

    My code in the question is working as expected in android and UWP, but not working well in ios.

  • SreeeeSreeee INMember ✭✭✭✭✭
    edited October 2019 Accepted Answer

    @LeonLu Solved :)

    public partial class MainPage : ContentPage
    {
      int year, month, day;
    
      public MainPage()
      {
         InitializeComponent();
    
         dateLabel.Text = DateTime.Now.ToString("dd-MM-yyyy");
    
         year = DateTime.Now.Year;
         month = DateTime.Now.Month;
         day= DateTime.Now.Day;
      }
    
      private void Button_Clicked(object sender, EventArgs e)
      {
        DateTime nowDate = new DateTime(year, month, day);
    
        var previewDate = nowDate.AddDays(-1);
    
        dateLabel.Text = previewDate.ToString("dd-MM-yyyy");
    
        year = previewDate.Year;
        month = previewDate.Month;
        day = previewDate.Day;
      }
    
      private void Button_Clicked_1(object sender, EventArgs e)
      {
         DateTime nowDate = new DateTime(year, month, day);
    
         var nextDate = nowDate.AddDays(+1);
    
         dateLabel.Text = nextDate.ToString("dd-MM-yyyy");
    
         year = nextDate.Year;
         month = nextDate.Month;
         day = nextDate.Day;
      }
    }
    

    enter image description here

  • LeonLuLeonLu Member, Xamarin Team Xamurai

    @Sreeee my above reply is suitable for you as well, you can test it. If so, you can mark it as well.

  • SreeeeSreeee INMember ✭✭✭✭✭

    @LeonLu said:
    @Sreeee my above reply is suitable for you as well, you can test it. If so, you can mark it as well.

    Sure :)

  • LeonLuLeonLu Member, Xamarin Team Xamurai

    @Sreeee Thanks.

Sign In or Register to comment.