0

## 如何計算不包含特定月份的日期區間天數

``````\$scope.date1 =  new Date("2020-05-15");
\$scope.date2 =   new Date("2020-07-14")
\$scope.date3 = ( \$scope.date2 - \$scope.date1)  / 1000 / 60 / 60 / 24+1;
``````

``````    \$scope.fm =  new Date("2019-05-15");
\$scope.to =   new Date("2019-07-14")
\$scope.all = ( \$scope.to - \$scope.fm)  / 1000 / 60 / 60 / 24+1;
\$scope.sfm =   new Date("2019-06-01")
\$scope.sto=new Date("2019-09-30")
if(\$scope.fm<\$scope.sfm&&\$scope.to>\$scope.sto){
\$scope.sum=((\$scope.to - \$scope.fm)-(\$scope.to - \$scope.sto)-(\$scope.sfm - \$scope.fm)) / 1000 / 60 / 60 / 24+1
}
if(\$scope.fm<\$scope.sfm&&\$scope.to<\$scope.sto){
\$scope.sum=((\$scope.to - \$scope.fm)-(\$scope.sfm-\$scope.fm)) / 1000 / 60 / 60 / 24+1
}
if(\$scope.fm>\$scope.sfm&&\$scope.to>\$scope.sto){
\$scope.sum=((\$scope.to - \$scope.fm)-(\$scope.to-\$scope.sto)) / 1000 / 60 / 60 / 24+1
}
if(\$scope.fm>\$scope.sfm&&\$scope.to<\$scope.sto){
\$scope.sum=( \$scope.to - \$scope.fm)  / 1000 / 60 / 60 / 24+1;
}
``````
goodnight iT邦研究生 3 級 ‧ 2020-07-24 16:00:05 檢舉

### 4 個回答

1
archer9080
iT邦新手 4 級 ‧ 2020-07-24 15:28:58

amicanon9 iT邦新手 5 級 ‧ 2020-07-24 15:34:26 檢舉

0

A~B = 實際天數範圍
O~P = 夏日範圍

ABOP 夏日=0 一般=B-A(全)
AOBP 夏日=B-O 一般=O-A
AOPB 夏日=OP(全) 一般=(O-A)+(B-P)
OAPB 夏日=P-A 一般=B-P
OPAB 夏日=0 一般=B-A(全)

0

iT邦研究生 2 級 ‧ 2020-07-24 21:37:05

``````    let arr=[
{dat: 計算起始日期, tpy: "start"},
{dat: 計算結束日期, tpy: "end"},
{dat: 夏日電費起始, tpy: "priceChange", val: 夏日電費},
{dat: 夏日電費結束+1天, tpy: "priceChange", val: 平日電費}
];
arr.sortByDat(); //arr 依照 dat去排序
let price=平日電費,
susm=0,
lastDate=false;
for(data in arr) {
//依據收到的 data.typ 計算，更新 sum, price, lastDate
}
``````
1
japhenchen
iT邦大師 1 級 ‧ 2020-07-27 11:37:00

.........回錯.....等人刪