简体   繁体   中英

How can I replace the specific character beginning and end of string?

I used moment to get longDateFormat. Like this:

moment.locale(navigator.language || navigator.userLanguage);
let dateFormat = moment.localeData().longDateFormat("L");

And I want get format of day and month.For ex:

DD/MM or MM/DD<br>
DD,MM or MM,DD<br>
etc... <br>

I used DATE_NO_YEAR = dateFormat.replace(/[y,Y]/g, "") to get format for date no year. But have one character at beginning or end of string.
How can I replace string or any solution that I can get format for day and month?

This will almost certainly not capture all the locales in Moment, but it might be a good start:

var f = (s) => s.replace(/Y/gi, '').replace(/^[^MD]|[^MD]$/gi, '');

f('MM/DD/YYYY'); //=> "MM/DD"
f('YYYY-MM-DD'); //=> "MM-DD"
f('DD/MM/YYYY'); //=> "DD/MM"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM