I have this function in JS and its a very strange because works with many dates but with one not..
I have two inputs and with jquery copy the first in the second if the second is minor or is null, and then if i put another greater date in the first, the second date changes to the same at the first.
But I have one case that the first input has 01/05/2013 and in the second has 31/05/2013, if i change the first to 01/06/2013 the second should be change at the same date, but dont do it..
And if with firebug i test it and this function
obtenerFecha(fIni) > obtenerFecha(fFin) // obtenerFecha(01/06/2013) > obtenerFecha(31/05/2013)
returns false, and its true!
Here is my code if someone can help me..
function copiarFecha(inicio,fin) {
var fIni = $("#" + inicio).val();
var fFin = $("#" + fin).val();
if ((fFin == "") || obtenerFecha(fIni) > obtenerFecha(fFin)) {
$("#" + fin).attr('value', fIni);
}
return true;
}
function obtenerFecha(strFecha) {
var df = strFecha.split("/");
if (isNaN(df[0]) || isNaN(df[1]) || isNaN(df[2]) || (df[0] > 31) || (df[1] > 12)) {
cargaMensaje("KO", "La fecha no es correcta,revísela.");
return false;
}
return new Date(df[2], df[1], df[0]);
Thanks.
You need to subtract 1 from the months. http://jsbin.com/eqozic/2/
month
Integer value representing the month, beginning with 0 for January to 11 for December.
https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date