new Date()
This date '21/10/1984', does not works in new Date(). Have snome problem on because the day created is 20 but setFullYear function works fine.
function validarData(){ valor = '21/10/1984'; // <= dd/mm/yyyy format var formatoValido = new RegExp(/\b\d{1,2}[\/-]\d{1,2}[\/-]\d{4}\b/); // format var valido = false; // default = false
if(formatoValido.test(valor)) { // check format var dia = valor.split("/")[0]; // get day var mes = valor.split("/")[1]; // get month var ano = valor.split("/")[2]; // get year var MyData = new Date(ano, mes - 1, dia); // value will be '20/10/1984'??? why day 20? <=============== // MyData.setFullYear(ano,mes - 1,dia); < uncomment this to work... setFullYear set the correct day <=============== if((MyData.getMonth() + 1 != mes)|| (MyData.getDate() != dia)|| (MyData.getFullYear() != ano)) $('#idmensagensform').text('Ops, Data incorreta.'); // warning incorrect date else valido = true; // set valid } if(valido == false){ $("#nascimento").focus(); } return valido; }
Promblem affects versions: 10.0 + Windows 7 10.0.2 + Windows 7
Theses problems not happens in FF 11 + Ubuntu 11.10
Усі відповіді (1)
Perhaps because my system uses mm/dd/yyyy I cannot replicate this error. Hopefully someone who uses dd/mm/yyyy can take a look. I think for purposes of testing, this is the essential part:
<html><head> <script type="text/javascript"> function datetest(){ valor = '21/10/1984'; // dd/mm/yyyy format var dia = valor.split("/")[0]; // get day var mes = valor.split("/")[1]; // get month var ano = valor.split("/")[2]; // get year var MyData = new Date(ano, mes - 1, dia); document.body.innerHTML += '<p>Computed new date (d/m/y) = '+MyData.toString()+'</p>'; valor = '10/21/1984'; // mm/dd/yyyy format var dia = valor.split("/")[1]; // get day var mes = valor.split("/")[0]; // get month var ano = valor.split("/")[2]; // get year var MyData = new Date(ano, mes - 1, dia); document.body.innerHTML += '<p>Computed new date (m/d/y) = '+MyData.toString()+'</p>'; } window.onload = datetest; </script> </head> <body></body> </html>