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
  • 7,307
  • 2
  • 32
  • 56

1 Answers1

1

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 Ross
  • 13,086
  • 11
  • 53
  • 88