question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

toLocaleString wrong format on Android

See original GitHub issue

Is this a bug report?

Yes

Have you read the Contributing Guidelines?

Yes

Environment

Environment: OS: macOS Mojave 10.14.1 Node: v11.1.0 Yarn: 1.12.3 npm: 6.4.1 Watchman: 4.9.0 Xcode: Xcode 10.0 Build version 10A255 Android Studio: 3.1.3 Build #AI-173.4819257

Packages: (wanted => installed) react: 16.4.1 => 16.4.1 react-native: 0.56.0 => 0.56.0

Target Platform: Android (8.0)

Steps to Reproduce

  1. Open a fresh React Native project
  2. Make sure you’re not debugging JS remotely
  3. Try to use toLocaleString on a Date() object
  let formatted = new Date('2017-11-17T10:59:30.527Z').toLocaleString('de-DE', {
    timeZone: 'Europe/Zurich',
    year: 'numeric',
    month: 'numeric',
    day: 'numeric',
    hour: 'numeric',
    minute: 'numeric',
  })
  1. Either write up a simple component to display text or do an alert/console.log with the aforementioned variable.

Expected Behavior

The expected string should be as follows:

Imput: 2017-11-17T10:59:30.527Z -> Output: 17.11.2017, 11:59

Actual Behavior

The resulting string is as follows:

Imput: 2017-11-17T10:59:30.527Z -> Output: Fri Nov 17 11:59:30 2017

My contention is that this is an issue with the underlying JS engine that Android uses, and handles it differently as opposed to the integrated JS engine on iOS, hence the variance in the date format. For this reason if you open a remote debugged, say on Chrome, you will actually get the correct format, due to the different JS engine handling it, but if you switch off the debugger and resume using the application whilst not debugging remotely, the incorrect format will be shown.

Reproducible Demo

Here’s an example Expo project that shows the behaviour, toggle between iOS and Android to see the difference in the output.

https://snack.expo.io/Bk3unwhyM

EDIT: Updated environment in which this is still reproducible.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:25
  • Comments:18 (1 by maintainers)

github_iconTop GitHub Comments

24reactions
oriharelcommented, Jan 17, 2018

This issue must exist.

17reactions
kholiavko-romancommented, Feb 24, 2018

Issue still present in the latest version.

Read more comments on GitHub >

github_iconTop Results From Across the Web

react-native .toLocaleString() not working on android
This is an issue with Javascript core used to run react native in Android and ... In case you need to format number...
Read more >
Date.prototype.toLocaleString() - JavaScript - MDN Web Docs
The toLocaleString() method returns a string with a language-sensitive representation of this date.
Read more >
Date & number formats | Google Sheets
Date-time and number formats let you control how your data appears in a sheet. Google Sheets provides some common formats to choose from,...
Read more >
Date | Android Developers
String · toLocaleString(). This method was deprecated in API level 15. As of JDK version 1.1, replaced by DateFormat.format(Date date) .
Read more >
Demystifying DateTime Manipulation in JavaScript - Toptal
Now, we get the correct date in MM/DD/YYYY format using: ... Use the toLocaleString() or toLocaleDateString() and toLocaleTimeString() methods or a date ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found