1
I have two codes to return the day of the week, but the first returns undefined, and the second returns the wrong day, for example on 22/05/2016 returns Wednesday, and the correct would be Sunday.
Code 1:
$(document).ready(function(){
$("input#dataR").blur(function(){
var data = $("#dataR").val();
var teste = new Date(data);
var dia = teste.getDay();
var semana = new Array(6);
semana[0]='Domingo';
semana[1]='Segunda-Feira';
semana[2]='Terça-Feira';
semana[3]='Quarta-Feira';
semana[4]='Quinta-Feira';
semana[5]='Sexta-Feira';
semana[6]='Sábado';
alert(semana[dia]);
});
});
Code 2:
$(document).ready(function(){
$("input#dataR").blur(function(){
var data = $("#dataR").val();
var arr = data.split("/");
var teste = new Date(arr[0],arr[1],arr[2]);
var dia = teste.getDay();
var semana = new Array(6);
semana[0]='Domingo';
semana[1]='Segunda-Feira';
semana[2]='Terça-Feira';
semana[3]='Quarta-Feira';
semana[4]='Quinta-Feira';
semana[5]='Sexta-Feira';
semana[6]='Sábado';
alert(semana[dia]);
});
});
In your second case, you are passing as parameters to the constructor of
Date
the values of day, month, year, but the order is the opposite (year, month, day). And note that in JS the months start from 0 (0 = January, 1 = February, ...)– carlosfigueira
@carlosfigueira I saw now your comment after putting the answer. Basically saying the same as you. Next you can put answer soon :)
– Sergio