Domanda

Ho fatto una funzione che cambia data se è un weekend ma invece di darmi il giorno successivo mi fa per me il giorno + 1 Ad esempio ho avuto un input con il 31-09-2012 che è stata l'ultima domenica.

Ho la mia funzione

<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>
.

Ma invece di darmi il primo ottobre mi ha dato il 31-09-2012 che non esiste.

Io davvero non so come elaborare.

È stato utile?

Soluzione

È necessario memorizzare il numero massimo di giorni al mese in array.

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

Meglio farlo in una funzione separata:

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;
}
.

Allora devi solo fare (ad esempio):

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

Modifica: È necessario aumentare il mese per 1 se ciò accade.E devi aggiungere 1 ora all'anno e impostare il mese a 1 se la vecchia data era 31-12-ayyy

Altri suggerimenti

prova data.js facile da trova fine settimana

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

2012/07/25 - Sabato.Controllare il codice se è sabato - aggiungi 2 giorni, se la sua domenica - Aggiungi 1 giorno 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)
.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top