Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't toLocaleDateString working in react-native (Android)?

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.

like image 578
S.j. Sakib Avatar asked Jul 18 '18 10:07

S.j. Sakib


People also ask

How do I change the date format in react native?

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.


1 Answers

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

enter image description here

like image 69
verunar Avatar answered Oct 14 '22 14:10

verunar