web-ridge / react-native-paper-dates

Smooth and fast cross platform Material Design date and time picker for React Native Paper
https://www.reactnativepaperdates.com
MIT License
642 stars 166 forks source link

When editing minutes in PM, it will reset to AM when updated #236

Closed bbaars closed 1 year ago

bbaars commented 1 year ago

Current behaviour

If a user selected 2:30pm and then goes back into the time picker modal and changes the time to 2:35pm and saves it, the time becomes 2:35am. The PM label in the modal is still highlighted, but the PM is not persisted once the modal is saved.

I believe the bug is around this: (since the previous time is not 0, it was 2:30pm it doesn't ever add back the 12 hours) https://github.com/web-ridge/react-native-paper-dates/blob/d84b6cccebc5ccdb3603c4d4d8c81d4ac376be57/src/Time/timeUtils.ts#L268

Expected behaviour

When a user is editing the time, it should maintain the am/pm when updating the minutes.

How to reproduce?

  1. Open the TimePicker modal and select 2:30pm and save/close it.
  2. Reopen the time picker and change the time to 2:35pm (only updating the time and not clicking anything else) & saving it.
  3. Time should be reported as 2:35am now. Can verify by printing out the values or reopening the modal.

Preview

CleanShot 2023-01-08 at 14 58 40

What have you tried so far?

Your Environment

software version
ios 16.2
android x
react-native 0.69.6
react-native-paper 5.1.2
node x.x.x
npm or yarn npm
expo sdk 46.0.8
iM-GeeKy commented 1 year ago

Hey @bbaars can you try this patch (using patch-package?)

diff --git a/node_modules/react-native-paper-dates/src/Time/timeUtils.ts b/node_modules/react-native-paper-dates/src/Time/timeUtils.ts
index ca837e3..dc5c8b1 100644
--- a/node_modules/react-native-paper-dates/src/Time/timeUtils.ts
+++ b/node_modules/react-native-paper-dates/src/Time/timeUtils.ts
@@ -265,7 +265,10 @@ export function toHourOutputFormat(
   if (is24Hour) {
     return newHours
   }
-  if ((!is24Hour && previousHours === 0) || newHours === 0) {
+  if (previousHours === 0 && newHours !== 0) {
+    return newHours - 12 < 0 ? newHours : newHours - 12
+  }
+  if (previousHours >= 12 && newHours < 12) {
     return newHours + 12
   }
   return newHours

I also created a PR if this seems to resolve the issue. I was a little short on time, so I tried to throw something together. Apologies in advance if this doesn't suffice 😬

iM-GeeKy commented 1 year ago

Closed by https://github.com/web-ridge/react-native-paper-dates/pull/237