I have a Text tag in a screen, which i use to show live time using JavaScript new Date() object. Its giving me 24 hour format even when i set "en-US" and hour12: true i have tried everything but it doesn’t seem to work.
I tried the same code in reactjs and it works fine there, i think the problem is with react-native here, maybe react-native doesn’t support toLocaleTimeString()
This all depends on the JavaScript engine that is in use. Let’s assume you’re using Chrome for the reactjs test. In that case you’re using the Chrome JavaScript engine. If you were using another browser you could potentially get different results.
The locales and options arguments customize the behavior of the function and let applications specify the language whose formatting conventions should be used.
In implementations that support the Intl.DateTimeFormat API, these parameters correspond exactly to the Intl.DateTimeFormat() constructor’s parameters. Implementations without Intl.DateTimeFormat support are asked to ignore both parameters, making the locale used and the form of the string returned entirely implementation-dependent.
In React Native, the usual JavaScript engine is JavaScriptCore. If you search for something like:
javascriptcore "toLocaleTimeString"
you will find many links talking about this issue. e.g.:
These days there is another JavaScript engine that’s becoming popular on React Native called Hermes. I haven’t tried your code on Hermes, but the following page suggests that it might work for you:
toLocaleTimeString isn’t part of the core js library used in react-native for android. No idea why, it is for ios. I ended up using momentJs for this (and a few other) reasons to handle the localization.