Skip to content

fix(android): open material date picker on the value's day in the target timezone - #1078

Open
Tayyeb-max wants to merge 1 commit into
react-native-datetimepicker:masterfrom
Tayyeb-max:fix/android-material-initial-day
Open

Tayyeb-max wants to merge 1 commit into
react-native-datetimepicker:masterfrom
Tayyeb-max:fix/android-material-initial-day

Conversation

@Tayyeb-max

Copy link
Copy Markdown

Summary

Fixes #1077

RNMaterialDatePicker passed the raw value timestamp to MaterialDatePicker.Builder.setSelection(). The Material picker reads the selection as a UTC day (SingleDateSelector.setSelection → UtcDates.canonicalYearMonthDay), so when the value falls on a different calendar day in UTC than in the target timezone, the picker opens on the wrong day: a day early for zones ahead of UTC near midnight, a day late for zones behind UTC late in the evening.

The selection is now midnight UTC of the value's year, month and day in the target timezone (timeZoneName, timeZoneOffsetInMinutes or the device zone, through RNDate). This mirrors the conversion createNewCalendar already does for the selection coming back from the picker. It follows the approach suggested in the issue.

Test Plan

What's required for testing (prerequisites)?

Example app on an Android device or emulator in a timezone ahead of UTC, e.g. Asia/Karachi (UTC+5). Tested on a Pixel 7a, Android 17.

What are the steps to reproduce (after prerequisites)?

  1. Design material, mode time: set the time to 02:00. The value is now 2021-11-13T02:00+05:00 (2021-11-12T21:00Z).
  2. Mode date, input mode default, press "Show picker!".
  • Before: the picker opens on Nov 12, 2021.
  • After: the picker opens on Nov 13, 2021, and pressing OK keeps 2021-11-13T02:00+05:00.

The date math was also checked off-device for Europe/Brussels, Asia/Karachi, Asia/Kolkata, America/New_York and Pacific/Kiritimati (old selection on the wrong day in each) and UTC (unchanged).

Compatibility

OS Implemented
iOS ❌
Android ✅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Android: material date picker opens on the wrong day

1 participant