JSFiddle - React, Tailwind, and code Playground

by Sergio Crisostomo

JavaScript

// dia-mes-ano ou ano-dia-mes
function datar(str) {
    var partes = str.split(/[\/\-]/);
    var date = partes[0].length == 4 ? new Date(partes[0], partes[2], partes[1]) : new Date(partes[2], partes[1], partes[0]);
    return date;
}

console.log(datar('01-10-2015')); // Sun Nov 01 2015 00:00:00 GMT+0100 (W. Europe Standard Time)
console.log(datar('2015/20/10')); // Fri Nov 20 2015 00:00:00 GMT+0100 (W. Europe Standard Time)