1

I'm building a web app that's going to target multiple locales. My app is written in TypeScript using Angular 2.

My user-facing date controls have a rich layout for each of the month/day/year boxes. I'm using some code in my template to order the M/D/Y boxes according to the user's locale (e.g. 'DD/MM/YYYY' for en-CA, 'MM/DD/YYYY' for en-US).

At the moment, I've got a huge array of locales that I defined once (https://stackoverflow.com/a/9893752/106639) and then match the browser language locale to get the formatting string. It feels like a ham-fisted approach for what I'm doing.

95% of what I need to do is within MomentJs, and my controllers and tests work just fine regardless of the formatting string.

Unfortunately, without knowing the formatting string, I don't know how to set the order of my UI components to match the user's expectations (e.g. if I expect DD/MM/YYYY, it doesn't do me much good if everything respects DD/MM/YYYY except the visible UI control).

Moment exposes some locale data (http://momentjs.com/docs/#/i18n/locale-data/), but it seems to be more about locale-specific projections of the actual date rather than the formatting string.

My ideal drop-in solution would be something like moment().localeData().dateFormatString, which would return me "DD/MM/YYYY".

Or, if I seem to be entirely on the wrong track for handling this sort of UI situation, some pointers would be appreciated!

Community
  • 1
  • 1
Stefan Mohr
  • 2,170
  • 2
  • 23
  • 35
  • 1
    `moment.localeData().longDateFormat('L')`: http://momentjs.com/docs/#/i18n/locale-data/ – JB Nizet Jan 09 '17 at 20:59
  • That works! The missing magic sauce was to update to Moment 2.17.1. I was previously on 2.15 and only had access to a moment.localeData()._longDateFormat() function, and the prefix underscore made me nervous.. In 2.17 it's accessible exactly as your code is composed. Thank you! – Stefan Mohr Jan 09 '17 at 21:22

0 Answers0