ThuyNguyen
ThuyNguyen

Reputation: 1185

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?

Upvotes: 0

Views: 69

Answers (1)

Scott Sauyet
Scott Sauyet

Reputation: 50807

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"

Upvotes: 2

Related Questions