Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get moment.js separator according to different locale?

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.

like image 252
huan feng Avatar asked Feb 04 '26 10:02

huan feng


1 Answers

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');
like image 136
Ilia Avatar answered Feb 06 '26 00:02

Ilia