[TIMOB-26858] Android (V8): toLocaleDateString has different outputs on iOS and Android
| GitHub Issue | n/a |
|---|---|
| Type | Bug |
| Priority | High |
| Status | Closed |
| Resolution | Duplicate |
| Resolution Date | 2020-05-11T23:15:57.000+0000 |
| Affected Version/s | n/a |
| Fix Version/s | n/a |
| Components | Android |
| Labels | engSchedule |
| Reporter | Hans Knöchel |
| Assignee | Alan Hutton |
| Created | 2019-02-25T10:28:53.000+0000 |
| Updated | 2020-05-11T23:15:57.000+0000 |
Description
When calling
toLocaleDateString on a Date object, the output is different on iOS and Android.
Test-Case (expecting German locale):
new Date(2019, 1, 25).toLocaleDateString('de-DE')
Output iOS (and Chrome):
25.02.2019
Output Android:
Mon Feb 25 2019
This is a parity issue for as, as we'd like to use localized dates for our users. Hopefully it's just a tweak in V8!
The real issue here is that Android's
Date.toLocaleDataString()method doesn't support a 2nd argument that allows you to specify how you want the month, day, and year to appear. That is what you really need. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString For now, your only option is to use Titanium's [String.formatDate()](https://docs.appcelerator.com/platform/latest/#!/api/Global.String-method-formatDate) function. It will provide consistent results between platforms.Unfortunately, none of the format strings forString.formatDate()support aMM/DD/YYYYorDD/MM/YYYYformat where it shows leading zeros for the month or day or 4 digit years. I believe this is the format you are after. Or... perhaps another alternative solution is to hard-code the date format too YYYY-MM-DD. The below works on Android, but it doesn't work on iOS and I'm not sure why at the moment. (Would need to isolate later.)But right, supporting thevar date = new Date(Date.UTC(2019, 1, 5)); console.log(String.format("%04d-%02d-%02d", date.getFullYear(), date.getMonth() + 1, date.getDate())); // Android outputs: 2019-01-05 // iOS outputs: 000-00-07Date.setLocaleDateString()method's 2nd argument is the ultimate solution.Thanks for the input! For now, we went with the
date-fnsmethodformatthat takes a locale as well. How can it be that Android would not support this? Does it a ship with a different V8 revision?Part of the problem is that the Android NDK C/C++ side does not support locale at all. For example, C functions such as
setlocale()will no-op and the locale will always default to effectively "en-us" (ie: C++std::locale::classic). This is a limitation on Google's end. I'm pretty sure our only option is to override the JavaScript functions and do the locale formatting on the Java side.[~hknoechel], I'm closing this ticket in favor of [TIMOB-27892]. Sorry. I forgot about this ticket and wrote up a duplicate, but the good news is that the other ticket has a working PR that does what you want. :)