react-datepicker: throw out RangeError('Invalid time value')
Expected behavior
Hello, I want to use the component to select date, but it keeps throwing out Invalid time value error. No idea why.
Actual behavior
The below is where error was throw out.
if (!isValid(originalDate)) {
throw new RangeError('Invalid time value');
} // Convert the date in system timezone to the same date in UTC+00:00 timezone.
// This ensures that when UTC functions will be implemented, locales will be compatible with them.
// See an issue about UTC functions: https://github.com/date-fns/date-fns/issues/376
Steps to reproduce
<DatePicker
selected={this.state.date}
onChange={(value:any)=> {
// value here is javascript date object
this.setState({date: moment(value)})
}}
/>
I tested the above simple case, it throws the same error. Really confused.
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Comments: 38
@gamesover Thanks for the heads up. I was also passing in a moment object into the
selectedprop.Same issue but only happening on Safari, which is weird. In Firefox and Chrome is working as expected.
Edit: Adding
.replace(/-/g, '/')toevent.startseems to solve the problem for me.react-datepickerbefore version 2, it usemomentfor date type after version 2, it use js date typeplease pay attention whether the version of
react-datepickeris consist with the version of@types/react-datepicker. Typescript shall give you warning.It is very likely you pass
momentdate toreact-datepicker@2.5.0, which use js date type now.I had the same problem when binding the date picker to a field in an object that had been parsed from a fetch() call, something like this (in Typescript):
Being used to strongly typed languages, I had fallen into the trap of thinking that the json parser would convert the birthDate to a Date but of course json doesn’t know about date types so the birthDate field is just a string. I fixed it with
(technically this would also have the effect of replacing undefined with null, but that suited my purposes anyway)
i am facing same issue with simple datepicker i am initializing date with new Date()
Also using 2.9.6 and getting the same error. Consdering nobody cares about this error, I’ll just go use the other datepicker. Thanks for the awesome support!
moment().toDate()somehow does not work. But JS’sDate.parse()works:Date.parse(moment(dateString, 'MM/DD/YYYY').toISOString())works for me 👍
It`s worf for me
The selected prop takes directly the date object, hence we have to convert any date string to a date object. it can be done by passing
selected={new Date(dateInAnyString)}and it should workI resolved this by wrapping my string date into a Date object. This got rid of the error:
I fixed it by changing wherever I had to pass
nullwithundefinedinsteadI also faced same issue in my application. But I got the solution by reduce my version to react-datepicker": “2.3.0”
Please use this version and fix you issues.
I have same issue only in Safari. I think it happens because Safari doesn’t support
<input type='time'>When I delete the colon, date cannot be parsed. I added this code in onChange listener:if (!(dateIn instanceof Date && !isNaN(dateIn))) return;Instead of an error, the previous date is selected until the user enters a new date. It helps me, so I hope it helps you too.I’m also seeing the same thing when I try to initialize
selectedwithnew Date(). It works if I leave it un-initialized.I am also getting the same issue with
react-datepicker@2.5.0Any luck?