Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

toLocaleDateString() does not work in Android. The date format gets trimmed from 'yyyy' to 'yy' (iOS does work correctly) #29824

Closed
RSP531 opened this issue Sep 1, 2020 · 5 comments
Labels
JavaScript Needs: Attention Issues where the author has responded to feedback. Platform: Android Android applications. Resolution: Locked This issue was locked by the bot.

Comments

@RSP531
Copy link

RSP531 commented Sep 1, 2020

Description

When using Dates in React Native, the method toLocaleDateString() clips the date. Is there a work around for android?

React Native version:

"react-native": "~0.62.2",

Steps To Reproduce

In any component paste this code and you will see...

      let todaysDate = new Date();
      let [month, day, year] = todaysDate.toLocaleDateString().split("/")
      console.log('Year: ', year)
      //this was my workaround
      if (Platform.OS === 'android') {
        year = todaysDate.toString().slice(11, 15)
      }
      console.log('Year: ', year)

Expected Results

The first year will log a 2 digit year: '20'. In iOS the year will log a 4 digit year: '2020'

Snack, code example, screenshot, or link to a repository:

//sorry I do not have an open repo or snack

@react-native-bot react-native-bot added Platform: Android Android applications. Platform: iOS iOS applications. labels Sep 1, 2020
@chrisglein
Copy link

Put that in a Snack:
https://snack.expo.io/@chrisglein/repro29824

If you just want the year, getFullYear produces the 4 digit value. Parsing it out of toLocaleDateString is going to be dependent on the locale formatting per platform, which I guess is different on Android? Is this an RN issue or just a difference in Android/iOS defaults? (Web also displays the 4 digit value, like iOS)

@RSP531
Copy link
Author

RSP531 commented Sep 1, 2020

Maybe just an issue with Android/iOS defaults? I just noticed a difference on the two platforms, but couldn't find any information on RN or MDN to explain why the js function works differently on the platforms.

@github-actions github-actions bot added Needs: Attention Issues where the author has responded to feedback. and removed Needs: Author Feedback labels Sep 1, 2020
@0916dhkim
Copy link

@RSP531 I found a StackOverflow thread related to this issue.
https://stackoverflow.com/questions/51399551/why-isnt-tolocaledatestring-working-in-react-native-android

React Native used JavaScriptCore engine in non-debug mode & it doesn't work well with dates, but work during debug because it use chrome V8 engine while debugging.

@chrisglein
Copy link

I think this issue has been answered now between this and the StackOverflow thread. Closing this issue.

@VictorioMolina
Copy link

I am getting the same problem in development mode. Will this platform specific problem dissapear on production?

@facebook facebook locked as resolved and limited conversation to collaborators Oct 27, 2021
@react-native-bot react-native-bot added the Resolution: Locked This issue was locked by the bot. label Oct 27, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
JavaScript Needs: Attention Issues where the author has responded to feedback. Platform: Android Android applications. Resolution: Locked This issue was locked by the bot.
Projects
None yet
Development

No branches or pull requests

5 participants