주말 일 경우 날짜를 변경하는 기능
-
12-12-2019 - |
문제
주말이면 날짜를 변경하는 기능을 해왔으나 다음날 저에게 하루에 나에게 해주는 대신 + 1 일이 지난 일요일이었습니다.
내 기능이 있습니다
<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>
.
그러나 나에게 첫 번째 10 월을주는 대신 31-09-2012가 존재하지 않는 31-09-2012를 알려 줬습니다.
공정하는 방법을 알지 못합니다.
해결책
배열에서 한 달에 최대 일 수를 저장해야합니다.
var dayspermonth = new array(31,28,31,30,31,30,31,31,30,31,30,31);
.
별도의 함수에서 수행하는 것이 가장 좋습니다 :
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;
}
.
그런 다음 (예 :)해야합니다.
itemCount[i].value = (itemDt.getDate() < 9 ? "0" : "")+ lastDay((itemDt.getDate()+1), itemDt.getMonth())+ "-" + (itemDt.getMonth() < 9 ? "0" : "") + (itemDt.getMonth() + 1) + "-" + itemDt.getFullYear();
.
편집 : 그런 일이 발생하면 월 1 일을 늘려야합니다.그리고 오래된 날짜가 31-12-yyyy 에 1 년으로 1 년을 추가하고 1 개월을 1로 설정해야합니다.
다른 팁
date.js 주말 찾기
Date.today().is().weekday() // Is today a weekday?
2012/07/25 - 토요일.코드가 토요일에 있는지 확인하십시오 - 일요일이면 2 일 추가하십시오 - 1 일 추가 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)
. 제휴하지 않습니다 StackOverflow