For some strange reasons, toLocaleDateString
isn't working properly in react-native. Sometimes it works, sometimes it doesn't. For the following code,
const dateString = this.state.date.toLocaleDateString('en-US', {
weekday: 'short',
day: 'numeric',
month: 'long',
year: 'numeric',
});
Sometimes I get just 07/17/2018
and sometimes regular output. Now I can implement it myself or use moment.js
or something like that. I want to know why this is behaving like that.
In your component: import { format } from "date-fns"; var date = new Date("2016-01-04 10:34:23"); var formattedDate = format(date, "MMMM do, yyyy H:mma"); console. log(formattedDate); Substitute the format string above "MMMM do, yyyy H:mma" with whatever format you require.
I am currently on "react-native": "~0.63.3",
and have the same issue when using toLocaleDateString. Here is what fixed it for me:
In my android/app/build.gradle file I replaced the following line
def jscFlavor = 'org.webkit:android-jsc:+' //remove this - it might be something else depending on your react-native version, try to look in your build.gradle file for similar comment I posted below in the picture
with
def jscFlavor = 'org.webkit:android-jsc-intl:+' // add this
The solution was found here: https://github.com/react-community/jsc-android-buildscripts#international-variant
and in my build.gradle file there is a comment from react-native, that describes that issue
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With