簡體   English   中英

如何以小時為單位計算兩個日期之間的差異,省略周末的時間?

[英]How can I get the difference between two dates in hours, omitting the hours on weekends?

我需要一個函數來檢查兩個日期之間已經過去了多少小時,但是如果例如中間有一個周末,那么就跳過這些時間。

日期1: moment('2022-05-27 20:00:00).tz('UTC')

日期2: moment('2022-05-30 20:00:00).tz('UTC')

預期輸出: 24 hours

如果我做i <= n ,我得到 25,當我做 i i < n n 時,我得到 24

但我不使用時刻

請注意,這應該適用於您所在地區的夏令時更改

 const d1 = new Date('2022-05-27 20:00:00') const d2 = new Date('2022-05-30 20:00:00') console.log(d1, d2) let hours = 0; for (let i = d1.getTime(), n = d2.getTime(); i < n; i += 3600000) { const d = new Date(i); if (![0, 6].includes(d.getDay())) hours++; } console.log(hours)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM