Pergunta

Eu já fez uma função que alterar a data se for um fim de semana, mas em Vez de dar-me no dia seguinte ela me dia+1, por exemplo, eu tinha uma entrada com o 31-09-2012 qual foi o último domingo.

Eu tenho a minha função

<script type="text/javascript">
    function getdate2() {
        var items = new Array();
        var itemCount = document.getElementsByClassName("datepicker hasDatepicker");

        for (var i = 0; i < itemCount.length; i++) {
            items[i] = document.getElementById("date" + (i + 1)).value;
        }



        for (var i = 0; i < itemCount.length; i++) {
            items[i] = document.getElementById("date" + (i + 1)).value;
            var itemDtParts = items[i].split("-");
            var itemDt = new Date(itemDtParts[2], itemDtParts[1] - 1, itemDtParts[0]);
                    if (itemDt.getDay() == 6) {

                itemCount[i].value = (itemDt.getDate() < 9 ? "0" : "")+ (itemDt.getDate()+2)+ "-" + (itemDt.getMonth() < 9 ? "0" : "") + (itemDt.getMonth() + 1) + "-" + itemDt.getFullYear();


            }
                        if (itemDt.getDay() == 0) {

               itemCount[i].value = (itemDt.getDate() < 9 ? "0" : "")+ (itemDt.getDate()+1)+ "-" + (itemDt.getMonth() < 9 ? "0" : "") + (itemDt.getMonth() + 1) + "-" + itemDt.getFullYear();


            }

        }
       return items;
       }
</script>

Mas em Vez de dar-me a primeiro de outubro, ele tinha me dar o 31-09-2012 que não existe.

Eu realmente não sei como processo.

Foi útil?

Solução

Você precisa para armazenar o número máximo de dias por mês na matriz.

var dayspermonth = new array(31,28,31,30,31,30,31,31,30,31,30,31);

Melhor fazê-lo em uma área separada da função:

function int lastDay(d, m){
    var result = d
    var dayspermonth = new array(31,28,31,30,31,30,31,31,30,31,30,31);
    if(d > dayspermonth[m]){
        d=1;
    }
    return d;
}

Em seguida, você apenas tem que fazer (por exemplo):

itemCount[i].value = (itemDt.getDate() < 9 ? "0" : "")+ lastDay((itemDt.getDate()+1), itemDt.getMonth())+ "-" + (itemDt.getMonth() < 9 ? "0" : "") + (itemDt.getMonth() + 1) + "-" + itemDt.getFullYear();

Editar: Você precisa aumentar o mês por 1, se o que acontece.E você precisa adicionar 1 para o ano e mês definido para 1, se o velho data de 31-12-AAAA

Outras dicas

tente date.js fácil encontrar o fim-de-semana

Date.today().is().weekday() // Is today a weekday?

2012/07/25 - sábado.Código de verificar se o seu sábado - adicionar 2 dias, se o seu domingo - adicionar 1 dia http://jsfiddle.net/S3Q4P/

var mydate=new Date(2012,07,25)
if(mydate.getDay() == 6) //Saturday
mydate =new Date(mydate.getFullYear(),mydate.getMonth(),mydate.getDate()+2)
else if (mydate.getDay() == 0) //Sunday
     mydate =new Date(mydate.getFullYear(),mydate.getMonth(),mydate.getDate()+1)
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top