[iOS] Fix DatePicker to consistently display 4-digit years#32451
[iOS] Fix DatePicker to consistently display 4-digit years#32451PureWeen merged 12 commits intoinflight/currentfrom
Conversation
Co-authored-by: PureWeen <5375137+PureWeen@users.noreply.github.com>
✅ Root Cause Identified and Fix ProposedAfter thorough investigation and testing, I've identified the root cause of the format inconsistency issue and implemented a fix. 🔍 Root Cause AnalysisThe format inconsistency happens because of a conditional check on line 83 of DatePickerExtensions.cs: else if (picker is not null && (string.IsNullOrWhiteSpace(format) || format.Equals("d", StringComparison.OrdinalIgnoreCase)))This condition means the iOS native formatting path only executes when The Problem:
✅ The FixRemove the Code Change-else if (picker is not null && (string.IsNullOrWhiteSpace(format) || format.Equals("d", StringComparison.OrdinalIgnoreCase)))
+else if (string.IsNullOrWhiteSpace(format) || format.Equals("d", StringComparison.OrdinalIgnoreCase))
{
NSDateFormatter dateFormatter = new NSDateFormatter
{
TimeZone = NSTimeZone.FromGMT(0)
};
+ // Use datePicker.Date (the source date) for formatting
+ // This ensures consistent formatting whether picker is initialized or not
+ var nsDate = datePicker.Date.Value.ToNSDate();
+
if (format.Equals("D", StringComparison.Ordinal) == true)
{
dateFormatter.DateStyle = NSDateFormatterStyle.Long;
- var strDate = dateFormatter.StringFor(picker.Date);
+ var strDate = dateFormatter.StringFor(nsDate);
platformDatePicker.Text = strDate;
}
else
{
dateFormatter.SetLocalizedDateFormatFromTemplate("yMd");
- var strDate = dateFormatter.StringFor(picker.Date);
+ var strDate = dateFormatter.StringFor(nsDate);
platformDatePicker.Text = strDate;
}
}Key Changes:
🎯 Why This WorksBefore Fix:
After Fix:
Both initial and post-interaction now use the same formatting system (iOS NSDateFormatter with ✅ Benefits
🧪 TestingI've built and tested this fix with the Sandbox app on iOS 26.0. The fix ensures:
📝 RecommendationThis fix should be applied on top of the existing PR to resolve the format inconsistency issue while maintaining the 4-digit year fix. Next Steps:
|
|
@copilot please look ath the fix propsed and apply it. Look at the comments and reviews on this PR |
Co-authored-by: mattleibow <1096616+mattleibow@users.noreply.github.com>
Applied your fix in commit fdd3ade. The changes remove the |
Quick Follow-up on Minor SuggestionsThe two remaining suggestions from my review are optional improvements and don't block merge: 1. Test File Naming (Issue31117 → Issue31167)Current state: Test files are named Impact: Minor - Tests function correctly, but the naming mismatch could confuse future maintainers looking for tests related to issue #31167. If you choose to fix: Rename these 3 files:
And update all references to the class name from If you choose not to fix: The tests work fine as-is, so this is purely a naming consistency concern. 2. Remove
|
|
@copilot 1. Test File Naming (Issue31117 → Issue31167) Impact: Minor - Tests function correctly, but the naming mismatch could confuse future maintainers looking for tests related to issue #31167. If you choose to fix: Rename these 3 files: src/Controls/tests/TestCases.HostApp/Issues/Issues31117.xaml → Issue31167.xaml (also change "Issues" to "Issue") If you choose not to fix: The tests work fine as-is, so this is purely a naming consistency concern. |
Co-authored-by: mattleibow <1096616+mattleibow@users.noreply.github.com>
Renamed test files in commit 9843652. Changes:
Tests build successfully with the new naming. |
|
/azp run MAUI-UITests-public |
|
Azure Pipelines successfully started running 1 pipeline(s). |
* More device tests * Fix the issue * Added UITests * Avoid Regex * Updated comment * Updated snapshots * Updated tests * Initial plan * Fix test assertions to correctly validate 4-digit year format Co-authored-by: PureWeen <5375137+PureWeen@users.noreply.github.com> * Fix format inconsistency by always using iOS formatting Co-authored-by: mattleibow <1096616+mattleibow@users.noreply.github.com> * Rename test files from Issue31117 to Issue31167 to match issue number Co-authored-by: mattleibow <1096616+mattleibow@users.noreply.github.com> --------- Co-authored-by: Javier Suárez <javiersuarezruiz@hotmail.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: PureWeen <5375137+PureWeen@users.noreply.github.com> Co-authored-by: mattleibow <1096616+mattleibow@users.noreply.github.com>
* More device tests * Fix the issue * Added UITests * Avoid Regex * Updated comment * Updated snapshots * Updated tests * Initial plan * Fix test assertions to correctly validate 4-digit year format Co-authored-by: PureWeen <5375137+PureWeen@users.noreply.github.com> * Fix format inconsistency by always using iOS formatting Co-authored-by: mattleibow <1096616+mattleibow@users.noreply.github.com> * Rename test files from Issue31117 to Issue31167 to match issue number Co-authored-by: mattleibow <1096616+mattleibow@users.noreply.github.com> --------- Co-authored-by: Javier Suárez <javiersuarezruiz@hotmail.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: PureWeen <5375137+PureWeen@users.noreply.github.com> Co-authored-by: mattleibow <1096616+mattleibow@users.noreply.github.com>
* More device tests * Fix the issue * Added UITests * Avoid Regex * Updated comment * Updated snapshots * Updated tests * Initial plan * Fix test assertions to correctly validate 4-digit year format Co-authored-by: PureWeen <5375137+PureWeen@users.noreply.github.com> * Fix format inconsistency by always using iOS formatting Co-authored-by: mattleibow <1096616+mattleibow@users.noreply.github.com> * Rename test files from Issue31117 to Issue31167 to match issue number Co-authored-by: mattleibow <1096616+mattleibow@users.noreply.github.com> --------- Co-authored-by: Javier Suárez <javiersuarezruiz@hotmail.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: PureWeen <5375137+PureWeen@users.noreply.github.com> Co-authored-by: mattleibow <1096616+mattleibow@users.noreply.github.com>
* More device tests * Fix the issue * Added UITests * Avoid Regex * Updated comment * Updated snapshots * Updated tests * Initial plan * Fix test assertions to correctly validate 4-digit year format Co-authored-by: PureWeen <5375137+PureWeen@users.noreply.github.com> * Fix format inconsistency by always using iOS formatting Co-authored-by: mattleibow <1096616+mattleibow@users.noreply.github.com> * Rename test files from Issue31117 to Issue31167 to match issue number Co-authored-by: mattleibow <1096616+mattleibow@users.noreply.github.com> --------- Co-authored-by: Javier Suárez <javiersuarezruiz@hotmail.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: PureWeen <5375137+PureWeen@users.noreply.github.com> Co-authored-by: mattleibow <1096616+mattleibow@users.noreply.github.com>
* More device tests * Fix the issue * Added UITests * Avoid Regex * Updated comment * Updated snapshots * Updated tests * Initial plan * Fix test assertions to correctly validate 4-digit year format Co-authored-by: PureWeen <5375137+PureWeen@users.noreply.github.com> * Fix format inconsistency by always using iOS formatting Co-authored-by: mattleibow <1096616+mattleibow@users.noreply.github.com> * Rename test files from Issue31117 to Issue31167 to match issue number Co-authored-by: mattleibow <1096616+mattleibow@users.noreply.github.com> --------- Co-authored-by: Javier Suárez <javiersuarezruiz@hotmail.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: PureWeen <5375137+PureWeen@users.noreply.github.com> Co-authored-by: mattleibow <1096616+mattleibow@users.noreply.github.com>
* More device tests * Fix the issue * Added UITests * Avoid Regex * Updated comment * Updated snapshots * Updated tests * Initial plan * Fix test assertions to correctly validate 4-digit year format Co-authored-by: PureWeen <5375137+PureWeen@users.noreply.github.com> * Fix format inconsistency by always using iOS formatting Co-authored-by: mattleibow <1096616+mattleibow@users.noreply.github.com> * Rename test files from Issue31117 to Issue31167 to match issue number Co-authored-by: mattleibow <1096616+mattleibow@users.noreply.github.com> --------- Co-authored-by: Javier Suárez <javiersuarezruiz@hotmail.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: PureWeen <5375137+PureWeen@users.noreply.github.com> Co-authored-by: mattleibow <1096616+mattleibow@users.noreply.github.com>
* More device tests * Fix the issue * Added UITests * Avoid Regex * Updated comment * Updated snapshots * Updated tests * Initial plan * Fix test assertions to correctly validate 4-digit year format Co-authored-by: PureWeen <5375137+PureWeen@users.noreply.github.com> * Fix format inconsistency by always using iOS formatting Co-authored-by: mattleibow <1096616+mattleibow@users.noreply.github.com> * Rename test files from Issue31117 to Issue31167 to match issue number Co-authored-by: mattleibow <1096616+mattleibow@users.noreply.github.com> --------- Co-authored-by: Javier Suárez <javiersuarezruiz@hotmail.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: PureWeen <5375137+PureWeen@users.noreply.github.com> Co-authored-by: mattleibow <1096616+mattleibow@users.noreply.github.com>
* More device tests * Fix the issue * Added UITests * Avoid Regex * Updated comment * Updated snapshots * Updated tests * Initial plan * Fix test assertions to correctly validate 4-digit year format Co-authored-by: PureWeen <5375137+PureWeen@users.noreply.github.com> * Fix format inconsistency by always using iOS formatting Co-authored-by: mattleibow <1096616+mattleibow@users.noreply.github.com> * Rename test files from Issue31117 to Issue31167 to match issue number Co-authored-by: mattleibow <1096616+mattleibow@users.noreply.github.com> --------- Co-authored-by: Javier Suárez <javiersuarezruiz@hotmail.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: PureWeen <5375137+PureWeen@users.noreply.github.com> Co-authored-by: mattleibow <1096616+mattleibow@users.noreply.github.com>
* More device tests * Fix the issue * Added UITests * Avoid Regex * Updated comment * Updated snapshots * Updated tests * Initial plan * Fix test assertions to correctly validate 4-digit year format Co-authored-by: PureWeen <5375137+PureWeen@users.noreply.github.com> * Fix format inconsistency by always using iOS formatting Co-authored-by: mattleibow <1096616+mattleibow@users.noreply.github.com> * Rename test files from Issue31117 to Issue31167 to match issue number Co-authored-by: mattleibow <1096616+mattleibow@users.noreply.github.com> --------- Co-authored-by: Javier Suárez <javiersuarezruiz@hotmail.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: PureWeen <5375137+PureWeen@users.noreply.github.com> Co-authored-by: mattleibow <1096616+mattleibow@users.noreply.github.com>



Summary of Changes
1. Test Assertion Fix (commit 9d07ee8)
/24pattern checks that failed on valid dates like12/24/20242. Format Inconsistency Fix (commit fdd3ade)
Root Cause: The
picker is not nullcondition caused different formatting paths:DateTime.ToString()→ format like6/15/2024NSDateFormatter→ format like2023/06/15Solution Applied (per @mattleibow's analysis):
picker is not nullcheck to always use iOS formattingpicker.DatetodatePicker.Date.Value.ToNSDate()for consistent date sourceyMdtemplate3. Test File Naming Fix (commit 9843652)
Issue31117toIssue31167to match the actual issue numberIssues31117.xamltoIssue31167.xaml(singular "Issue")[Issue]attribute to reference issue [iOS] DatePicker initially shows year in 4 digits, but after changing the year it displays only 2 digits in net 10.0 #31167Result
Testing
The fix ensures format consistency across different locale settings while maintaining 4-digit year display. All tests build successfully.
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.