Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-26858] Android (V8): toLocaleDateString has different outputs on iOS and Android

GitHub Issuen/a
TypeBug
PriorityHigh
StatusClosed
ResolutionDuplicate
Resolution Date2020-05-11T23:15:57.000+0000
Affected Version/sn/a
Fix Version/sn/a
ComponentsAndroid
LabelsengSchedule
ReporterHans Knöchel
AssigneeAlan Hutton
Created2019-02-25T10:28:53.000+0000
Updated2020-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!

Comments

  1. Joshua Quick 2019-02-25

    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.
       var date = new Date(Date.UTC(2019, 1, 31));
       console.log(String.formatDate(date, "short"));
       // "en-us" output: 1/31/19
       
    Unfortunately, none of the format strings for String.formatDate() support a MM/DD/YYYY or DD/MM/YYYY format 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.)
       var 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-07
       
    But right, supporting the Date.setLocaleDateString() method's 2nd argument is the ultimate solution.
  2. Hans Knöchel 2019-02-25

    Thanks for the input! For now, we went with the date-fns method format that takes a locale as well. How can it be that Android would not support this? Does it a ship with a different V8 revision?
  3. Joshua Quick 2019-02-25

    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.
  4. Joshua Quick 2020-05-11

    [~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. :)

JSON Source