SfDatePicker shows the day and month swapped

Description

I want to use the SfDatePicker to allow the user to choose a date. I use a custom date format (dd-MM-yyyy) to have it match the Dutch date format.

However, after binding this control to a DateTime value (I used the date: September 4, 2021), the SfDatePicker shows the day and month swapped.

Is there a way to have the SfDatePicker work with such custom date format?


Steps to reproduce

  1. Create and initialize a DateTime: StartDate = new DateTime(2021, 09, 04);
  2. Bind the SfDatePicker to this DateTime value and set a custom stringformat:
  3. Start the SfDatePicker. It shows april 9th instead of september 4th.


Configuration

Xamarin.Forms v5.0.0.2012

Syncfusion.Xamarin.SfPicker v18.3.0.50 (also tested it with the latest version)

I added the reproduction project as an attachment.



Attachment: XamarinSfDatePicker_824bef72.zip


3 Replies 1 reply marked as answer

JK Jeya Kasipandi Syncfusion Team July 20, 2021 01:33 PM UTC

Hi Richard,

We have validated your query and we suggest you use converter in DateFormat conversion as like below code snippet to resolve the reported issue.

Code snippet:

C#:
public class DatetimeToStringConverter : IValueConverter
    {
        #region IValueConverter implementation

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
                return string.Empty;

            var datetime = (DateTime)value;
            var text = string.Format("{0:dd-MM-yyyy}", datetime);
           
            return text;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
                return DateTime.Now;
            CultureInfo provider = CultureInfo.InvariantCulture;
            var dd = value.ToString();
            DateTime dateTime = DateTime.ParseExact(dd,"dd-MM-yyyy", provider);
            return dateTime;
        }

        #endregion
    }

Xaml:

  <ResourceDictionary>
            <local:DatetimeToStringConverter x:Key="cnvDateTimeConverter"></local:DatetimeToStringConverter>
 </ResourceDictionary>
... 
 <Entry Text="{Binding StartDate,Converter={StaticResource cnvDateTimeConverter}}" x:Name="entry" IsReadOnly="True" InputTransparent="False"/>
...

We have modified the sample based on this and please find the sample from below link

Sample Link:

Please check with the above and let us know if you have any concern.

Regards,
Jeya K


Marked as answer

RI Richard July 26, 2021 12:32 PM UTC

That was indeed the solution, thank you!



JK Jeya Kasipandi Syncfusion Team July 27, 2021 06:56 AM UTC

Hi Richard,

Thanks for the update.

We are glad to know that the reported issue has been resolved.

Please let us know if you have any other queries.

Regards,
Jeya K

Loader.
Up arrow icon