3

The separator between month and day is different for different locale, how can I get it?

I want to display:

"MM/DD HH:mm" for English,

"MM-DD HH:mm" for Chinese,

"MM.DD HH:mm" for German.

How can I handle it with moment.js ?

Note: I could have lots of languages, I don't want to use if the check which language is currently used, and the format must be like what I have listed above.

huan feng
  • 6,189
  • 1
  • 26
  • 49

1 Answers1

0

The solution is straight forward. There are only three possible separators /, - and ..

Simply by checking, step by step, the string containing:

let LocalDate      = moment().format('l'),
    LocalSeparator = (LocalDate.indexOf('/') > -1 ? "/" : (LocalDate.indexOf('-') > -1 ? '-' : '.'));

Now you can build your own date/time, using proper separator based on locale:

moment().format('YYYY[' + LocalSeparator + ']MM');
Ilia
  • 12,670
  • 11
  • 49
  • 83