You can treat this as a string, or convert it to Date and show it with country rules.
Treating as string:
var formatoBr = '2020/05/10'.split('/').reverse().join('/');
console.log(formatoBr); // 10/05/2020
To convert and treat as Date you can do this:
var [ano, mes, dia] = '2020/05/10'.split('/').map(Number);
var date = new Date(ano, mes - 1, dia);
var formatoBr = date.toLocaleDateString('pt-BR');
console.log(formatoBr); // 10/05/2020
// ou ainda:
console.log(date.toLocaleDateString('pt-BR', {
weekday: 'long',
year: 'numeric',
month: 'long',
day: 'numeric'
})); // domingo, 10 de maio de 2020
If you always have 8 digits without tabs as you indicated in the question now you can do different ways in N ... a suggestion could be:
var formatoUS = '20200510';
var [ano, mes, dia] = formatoUS.match(/(\d{4})(\d{2})(\d{2})/).slice(1);
var formatoBr = [dia, mes, ano].join('/');
console.log(formatoBr); // 10/05/2020
You can also use type="date"
in input
, but in this case there are more limitations to formatting, as you give the browser the training task.