I have to format this date yyyy-mm-dd('2019-05-31') to m/dd/yy('5/31/19')
.
I have tried this
var format = function(input) {
var pattern = /(\d{4})\-(\d{2})\-(\d{2})/;
if (!input || !input.match(pattern)) {
return null;
}
return input.replace(pattern, '$2/$3/$1');
};
console.log(format('2019-05-31'));
But cannot eliminate the values.
.match()
as an indicator of a valid input store it in a variable and use it to build the string. Makes it easier to remove unnecessary numbers ;) – Andreas Jul 1 '20 at 8:15