react-native: new Date() bug
Is this a bug report?
Yes
Have you read the Contributing Guidelines?
No
Environment
-
react-native -v
: react-native-cli: 2.0.1 react-native: 0.46.4 -
node -v
: v7.10.0 -
npm -v
: 4.2.0 -
yarn --version
:
Then, specify:
- Target Platform: iOS 11.0
- Development Operating System: macOS Sierra
- Build tools: xCode9 beta
Steps to Reproduce
Not sure if it’s a react-native issue or iOS, but i’m setting a new Date(‘string’) -the string comes from the server- and it converts to a Date on iOS emulator, however when I run on device it becomes an invalid date (as I console logged on xCode).
here the logs:
the log scheme: console.log(‘string identifier’, ‘new Date(string)’, date string from server);
log commands: console.log(‘dtini’, dataInicio, this.props.programacaoLazer[i].data_inicio); console.log(‘dtfim’, dataFim, this.props.programacaoLazer[i].data_fim); console.log(‘dtselec’, this.state.dataSelecionada); log on emulator: dtini Tue Sep 05 2017 10:40:00 GMT-0300 (-03) 2017-09-05 10:40:00 dtfim Tue Sep 26 2017 13:18:00 GMT-0300 (-03) 2017-09-26 13:18:00 dtselec Tue Sep 05 2017 15:29:06 GMT-0300 (-03)
log on Xcode running on device: 2017-09-05 15:21:50.326 [info][tid:com.facebook.react.JavaScript] ‘dtini’, Invalid Date, ‘2017-09-05 10:40:00’ 2017-09-05 15:21:50.326 [info][tid:com.facebook.react.JavaScript] ‘dtfim’, Invalid Date, ‘2017-09-26 13:18:00’ 2017-09-05 15:21:50.327 [info][tid:com.facebook.react.JavaScript] ‘dtselec’, Tue Sep 05 2017 15:18:37 GMT-0300 (-03)
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 9
- Comments: 16 (2 by maintainers)
We’ve faced the same problem. For some reason JavaScriptCore doesn’t take “YYYY-MM-DD HH:MM:SS” format as a constructor param, (it does recognise “YYYY-MM-DD”, and another format mentioned below) Using moment.js might be an overkill, I wrote a tiny function that addresses this issue:
Same. Trying
new Date('2017-11-7, 6:37 pm')
with debugger off results inInvalid Date
with debugger on result is:Tue Nov 07 2017 18:37:00 GMT+0200 (EET)
yes i’m using chrome debugger on emulator.
i have fixed it by declaring the new Date as:
const dataInicio = new Date( parseInt(atividade.data_inicio.slice(0,4)), parseInt(atividade.data_inicio.slice(5,7)) - 1, parseInt(atividade.data_inicio.slice(8,10)), parseInt(atividade.data_inicio.slice(11,13)), parseInt(atividade.data_inicio.slice(14,16)), 0, 0 );
However it`s funny that it works on emulator but not on device
Are you using the Chrome debugger? (Make sure you aren’t using it for Date-related operations, V8 is different from JSC.)
I suggest this:
JSCore is not native to Android, It’s highly likely that the rebuilding might fix your problem.
I ended up with a slight modification of @SahRckr’s method to handle UTC dates.
This handles the format
2018-07-18 15:27:55 UTC
. Note that it will break for anything non-UTC.@SahRckr Rebuilding might solve the problem but I cannot rebuild the application. I am developing a feature in which if time on user’s device is incorrect (Criteria is if the time difference between mobile device time & time sent from server is more than 15 minutes),then I redirect him to Date & Time Settings in Android
That’s because react-native run javascript code using JavaScriptCore engine and it behaves like @Gigasz said, if anyone want to test code same way it would run in a real device, can install and use rhino
For me helped this:
moment( date & time , "YYYY-MM-DD hh:mm a").format("YYYY-MM-DDTHH:mm:ss")
You can try to see it in Safari -> development->agent->IOS device On IOS work good!