question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Date picker on iOS has different selected value then Date received in onChange callback

See original GitHub issue

Bug

DateTimePicker on iOS return in onChange method different then selected one. Different is one day.

Environment info

React native info output:

info Fetching system and libraries information...
System:
    OS: macOS 10.15.2
    CPU: (12) x64 Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz
    Memory: 128.34 MB / 16.00 GB
    Shell: 3.2.57 - /bin/bash
  Binaries:
    Node: 13.7.0 - /usr/local/bin/node
    Yarn: 1.21.1 - /usr/local/bin/yarn
    npm: 6.13.6 - /usr/local/bin/npm
    Watchman: 4.9.0 - /usr/local/bin/watchman
  SDKs:
    iOS SDK:
      Platforms: iOS 13.2, DriverKit 19.0, macOS 10.15, tvOS 13.2, watchOS 6.1
  IDEs:
    Android Studio: 3.5 AI-191.8026.42.35.6010548
    Xcode: 11.3.1/11C504 - /usr/bin/xcodebuild
  npmPackages:
    react: ^16.12.0 => 16.12.0 
    react-native: 0.61.4 => 0.61.4 
  npmGlobalPackages:
    create-react-native-app: 2.0.2
    react-native-cli: 2.0.1

Library version: 2.2.1

Steps To Reproduce

  1. Create simple app with DateTimePicker.
  2. Add item like this: <DateTimePicker testID="dateTimePicker" timeZoneOffsetInMinutes={0} value={parseDate(value)} mode={'date'} is24Hour={true} display="default" onChange={(event, date) => { debugger; console.log("Event: " + event.type); console.log("Selected date: " + date)); />
  3. Run it on iOS simulator. …

Describe what you expected to happen:

  1. Date on picker should be the same as it is in onChange callback function

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:21
  • Comments:17

github_iconTop GitHub Comments

28reactions
chrisidcommented, Aug 24, 2020

there is a “timeZoneOffsetInMinutes” prop, setting it to the user’s timezone offset solved this.

const offset = new Date().getTimezoneOffset() * -1;
<DateTimePicker
    timeZoneOffsetInMinutes={offset}
9reactions
ZE0TRONcommented, Jun 1, 2020

I am having the same issue both on android and ios the difference is one day. Edit: Solved the issue by using the toLocaleDateString() method

const onChange = (event, selectedDate) => {
    const currentDate = selectedDate || date;
    console.log(selectedDate.toLocaleDateString()); // Returns the correct date string you can convert it back to date if you need an date object.
    setModalVisible(Platform.OS === 'ios');
  };

It’s a temporary fix the issue needs to be resolved since the timezone offset has no effect in the date picker.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Date picker on iOS has different selected value then ... - GitHub
Describe what you expected to happen: Date on picker should be the same as it is in onChange callback function.
Read more >
Date Picker with UIControlEvents.valueChanged doesn't work ...
Strange. This appears to be a bug in date pickers that are set to UIDatePickerMode.countDownTimer mode. You can get around most of the...
Read more >
Solved: Onchange event often triggers when screen becomes
I get a notification that should only be displayed when a user is trying to change the date. It doesn't happen every time...
Read more >
React custom datepicker: Step-by-step - LogRocket Blog
Set up a robust and attractive datepicker in React that acts as an ... For example, with various date formats used globally, a...
Read more >
<input type="color"> - HTML: HyperText Markup Language
The value is never in any other form, and is never empty. ... the user re-opens the color picker), a change event is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found