Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Xamarin.Forms DatePicker shows incorrect date #13366

Closed
mikebikerider opened this issue Jan 11, 2021 · 15 comments · Fixed by #13634
Closed

Xamarin.Forms DatePicker shows incorrect date #13366

mikebikerider opened this issue Jan 11, 2021 · 15 comments · Fixed by #13634
Assignees
Labels
a/datepicker blocker Issue blocks next stable release. Prioritize fixing and reviewing this issue. in-progress This issue has an associated pull request that may resolve it! t/bug 🐛
Projects
Milestone

Comments

@mikebikerider
Copy link

Xamarin.Forms 5 iOS DataPicker shows incorrect date if UTC Date is ahead of the local Date. The default Date format "D" is incorrectly rendered: Day of Week is missing.

Steps to Reproduce in the Western Hemisphere

  1. Download the Xamarin DatePicker sample app 'Days Between Dates' and compile the iOS project.
  2. Run the app at the time when UTC Date is 1 day ahead of the local date. In the UTC - 5 time zone (EST US, Canada) that is after 19:00
  3. Look at the DatePicker - the date is one day behind of the local date. Tap the DatePicker to bring up the native iOS control. It shows the correct date. Select another date and click 'Done'. The date shown in the DatePicker is again 1 day behind.

Expected Behavior: The date shown in the control should be the date loaded or the date selected at all times. The Date format "D" (default) should be rendered with Day of Week before then day and month.

Actual Behavior: The date shown can be wrong depending on the local time, the format "D" is rendered without Day of Week

Basic Information

  • Version with issue: 5.0.0.1874
  • Last known good version: 4.8.0.1821
  • Platform Target Frameworks:
    • iOS: 14.3
    • Android: not affected
  • UWP: not relevant
  • Android Support Library / AndroidX Version:
  • NuGet Packages: Xamarin.Forms
  • Affected Devices: iPhone, iPad, simulator

Environment Visual Studio 2019 16.8.3

Show/Hide Visual Studio info

Build Logs

Screenshots

Reproduction Link IMG_0508

The screenshot made on January 8 at 19:02 EST (US, Canada) shows date January 7, 2021.
Is should show Friday, January 8, 2021.
A screenshot made same day before 19:00 shows correct date January 8, 2021 in wrong format.
The app is not mine. It is the Xamarin.Forms sample up downloaded from the GitHub one day after it was updated to Xamarin.Forms 5.0.0.1874. Downgrading to Xamarin.Forms to 4.8.0.1821 and running the app produced correct date in correct format "D". "D" is the default.

Workaround

Set DatePicker IsVisible = False;
In XAML or in code place a Label into the same position (grid cell) as the DatePicker.
In DatePicker.DateSelected add code to set label text to e.NewDate.ToString("D") or whatever date format you chose.
For the label add TapGestureRecognizer that will call DatePicker.Focus()

@mikebikerider mikebikerider added s/unverified New report that has yet to be verified t/bug 🐛 labels Jan 11, 2021
@samhouts samhouts added this to New in Triage Jan 11, 2021
@mikebikerider
Copy link
Author

Comparing DatePickerRenderer.cs for Xamarin.Forms 4.8 and 5.0 makes me think both issues - missing Day of Week for date format "D" and wrong date are caused by changes in UpdateDateFromModel(bool animate)
both start with:
if (_picker.Date.ToDateTime().Date != Element.Date.Date)
_picker.SetDate(Element.Date.ToNSDate(), animate);
In 4.8
Control.Text = Element.Date.ToString(Element.Format);
In 5.0
NSDateFormatter dateFormatter = new NSDateFormatter();
if (Element.Format?.Equals("D") == true)
{
dateFormatter.DateStyle = NSDateFormatterStyle.Long;
var strDate = dateFormatter.StringFor(_picker.Date);
Control.Text = strDate;
}

iOS date format long has no Day of Week.
Could be calling NSDateFormatter shifts displayed date by one day when local date is behind or ahead of the UTC date?

@educatedcg
Copy link

I agree this seems like a bug.. Perfectly working datetime picker no longer picks the correct date.
If you look at the binding property:

{1/31/2021 12:00:00 AM} - making matters worse, you can't even select the end of the month unless you select the first day of the next month.. It's always off by a day... Maybe the 12AM portion??

@mikebikerider
Copy link
Author

No, not the 12 AM portion. The underlying iOS DatePicker works correctly and always shows the correct date. The date is pushed 1 day back when control on the page is updated. Date is pushed 1 day back if you are in the Western hemisphere, I am. If I fly to the UTC + zone it would be 1 day ahead at least some hours of the day. It can be quite confusing.
The workaround I suggested is simple. The permanently displayed part of the DatePicker is just a label. Put your own label on top of it and make DatePicker not visible. Update your label in DatePicker.DateSelected event handler, and add TapGestureRecognizer for the label to focus the underlying iOS DatePicker. Look up the original post.
Of course it has to be fixed.
I still will keep my workaround because it gives me more control and better look.

@hoangwy
Copy link

hoangwy commented Jan 20, 2021

Same here
Simulator Screen Shot - iPhone 12 - 2021-01-19 at 18 07 03
As you can see
The Wheel shows January 19, 2021 but the label shows 1/18/21

@danielkboyer
Copy link

I am getting the same issue, this is frustrating, a working build is now halted because of this. @mikebikerider solution works, it's just a pain to implement this anywhere I have a DatePicker. Is there a fix that does not require creating a label over it?

@educatedcg
Copy link

It's crazy that no one is picking this bug up. It's a fundamental breaking bug. If no one picks it up, one of us will have to fix it. I had to revert my app to 4.8, because this make 5.0 unusable for me on iOS.

@PureWeen
Copy link
Contributor

I waited all day to test this issue and from what I can tell this is fixed with the latest SR1 of Xamarin.Forms

I'm closing this for now. If you still have an issue with the latest 5.0 release of Forms let me know and we can re-open

Triage automation moved this from New to Closed Jan 23, 2021
@PureWeen
Copy link
Contributor

@rachelkang is still seeing this issue where she's at in the world so going to re-open for now :-/

@PureWeen PureWeen reopened this Jan 23, 2021
Triage automation moved this from Closed to New Jan 23, 2021
@PureWeen PureWeen moved this from New to Ready For Work in Triage Jan 23, 2021
@PureWeen PureWeen added the blocker Issue blocks next stable release. Prioritize fixing and reviewing this issue. label Jan 23, 2021
@PureWeen PureWeen added this to the 5.0.0 milestone Jan 23, 2021
@PureWeen PureWeen added this to To do in vNext+1 (5.0.0) via automation Jan 23, 2021
@mikebikerider
Copy link
Author

The issue has to be caused by changes in DatePickerRendererBase.UpdateDateFromModel(bool animate) from Xamarin.Forms 4.8 to 5.0.
A second work around could be custom renderer overriding OnElementChanged() and OnElementPropertyChanged().
As an extra bonus you can pick a desired DatePicker style - Default, Wheels, Compact for iOS 13.4 and higher.

using YourAppName.iOS.Renderers;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
using UIKit;
using System.ComponentModel;

[assembly: ExportRenderer(typeof(DatePicker), typeof(MyDatePickerRenderer))]
namespace YourAppName.iOS.Renderers
{
public class MyDatePickerRenderer : DatePickerRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs e)
{
base.OnElementChanged(e);

        if (e.NewElement != null && this.Control != null)
        {
            try
            {
                UIDatePicker picker = (UIDatePicker)Control.InputView;
                if (UIDevice.CurrentDevice.CheckSystemVersion(13, 4))
                {
                    //select desired style
                    picker.PreferredDatePickerStyle = UIDatePickerStyle.Automatic;
                }
                UpdateDateFromModel(picker, false);

            }
            catch
            {
                // do nothing
            }
        }
    }
    protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        base.OnElementPropertyChanged(sender, e);
        try
        {
            try
            {
                UIDatePicker picker = (UIDatePicker)Control.InputView;
                UpdateDateFromModel(picker, true);

            }
            catch
            {
                // do nothing
            }
        }
        catch { }
    }
    private void UpdateDateFromModel(UIDatePicker picker, bool animate)
    {
        if (picker.Date.ToDateTime().Date != Element.Date.Date)
            picker.SetDate(Element.Date.ToNSDate(), animate);
        Control.Text = Element.Date.ToString(Element.Format);
    }
}

}

UpdateDateFromModel() - I pasted from the 4.8 DatePickerRendererBase

@Redth Redth moved this from Ready For Work to Needs Estimate in Triage Jan 25, 2021
@Westat-Transportation
Copy link

Westat-Transportation commented Jan 27, 2021

For the record, I am also seeing this issue in 4.8.0.1269. I also resolved this by applying @mikebikerider 's workaround.

@tommy-elliott-awh
Copy link

tommy-elliott-awh commented Jan 28, 2021

I have experienced this issue emulating in iOS on iPhone 12 iOS 14.3 on 1/27/21 in EST for the full duration between 11:40am EST and 5pm EST on Xamarin Forms 5 in VS 2019 v16.8.4 in Windows, connected to a Mac mini build agent with the latest Xcode 12. The formatted text date display "M/d/yyyy" was correct with today's date when initially loaded (today's date), but as soon as a change was made to any part of the date (I used the wheels style for the DatePicker), the formatted text date changed to represent one day before the selected date, even crossing month and year boundaries (displayed "12/31/2000" when 1/1/2001 had been selected, and similarly one day before what is selected for every date, regardless of selected date). After applying the workaround steps in @mikebikerider 's custom renderer workaround, the issue was resolved.

@hartez hartez removed this from Needs Estimate in Triage Feb 2, 2021
@hartez hartez removed this from Blockers in vNext+1 (5.0.0) Feb 2, 2021
@jsuarezruiz jsuarezruiz added the in-progress This issue has an associated pull request that may resolve it! label Feb 3, 2021
@jsuarezruiz jsuarezruiz moved this from To Fix to Issue In Progress in 5.0.0 SR 3 Feb 3, 2021
@PureWeen
Copy link
Contributor

PureWeen commented Feb 5, 2021

If anyone having this issue can test with the following nugets and let us know if it's resolved that would be helpful

https://dev.azure.com/xamarin/public/_build/results?buildId=34568&view=artifacts&pathAsName=false&type=publishedArtifacts

@mikebikerider
Copy link
Author

mikebikerider commented Feb 5, 2021 via email

5.0.0 SR 3 automation moved this from Issue In Progress to Done Feb 5, 2021
@educatedcg
Copy link

Did this make it into a nightly build? Tried 5.0.1.1876 - nightly build and saw the same effect with that binds a additional datefield to the selected date value

@tommy-elliott-awh
Copy link

@educatedcg It looks like @mikebikerider said

Xamarin.Forms build 5.0.0.6978 fixes the incorrect date issue.

which isn't in a nuget release yet, so I'm applying the workaround within the custom iOS renderer until it is available from nuget, and I would suggest you do the same.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
a/datepicker blocker Issue blocks next stable release. Prioritize fixing and reviewing this issue. in-progress This issue has an associated pull request that may resolve it! t/bug 🐛
Projects
No open projects
Development

Successfully merging a pull request may close this issue.

8 participants