0

多くの助けを借りて、日付が週末かどうかをオンロードでチェックし、日曜日の場合は日をインクリメントする必要がある機能を実行しました。土曜日の場合、残念ながらフランスでは誰も週末に働きたくないので、その日は 2 加算されます。

私が使用しているコードは次のとおりです。

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

        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()+ "-" + (itemDt.getMonth() < 9 ? "0" : "") + (itemDt.getMonth() + 1) + "-" + itemDt.getFullYear();


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

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

            }

        }
       return items;
  }
</script>

日付が週末かどうかを確認するためにはすべて正常に機能しますが、日付+1を尋ねても変更されません。

土曜日と日曜日を月曜日に変更したいだけです。

助けてくれてありがとう

SP。

4

1 に答える 1

1

どこで行うか:

> var itemDt = new Date(itemDtParts[2], itemDtParts[1] - 1, itemDtParts[0]);

次に、次のようなことを行うことができます。

// Increment date if Saturday or Sunday
var inc = itemDt.getDay() == 0? 1 : itemDt.getDay() == 6? 2 : 0;

// Only update DOM if necessary
if (inc) {
  itemDt.setDate(itemDt.getDate() + inc);
  // Update DOM
} 

日付が月末を超えて(または開始前に)設定されている場合、Dateオブジェクトは調整されるため、1月32日の日付を設定すると2月1日になります。

于 2012-08-29T07:18:32.450 に答える