簡體   English   中英

日歷日的差異,日期-fns/時刻以外的解決方案?

[英]difference in calendar days, solution other than date-fns/moment?

我試圖獲得兩個日期之間的天數差異,即 7 月 17 日和 7 月 19 日相差 2 天。 我目前正在使用 date-fns 庫並利用差異 InCalendarDays 不幸的是,這是不准確的,它返回 7 月 17 日和 7 月 19 日之間的 1 天差異。 我創建了這個 repl 來演示該方法的不准確功能。 我也嘗試過使用 momentjs 並得到相同的結果。

repl - https://repl.it/@yazmnh87/CumbersomeLightsalmonTheories#index.js

gitHub 問題日期-fns - https://github.com/date-fns/date-fns/issues/1877

我想到的一種解決方案是在保存到數據庫之前將時間設置為中午或其他時間,以便獲得一致的結果。 非常感謝有關如何輕松解決此問題的任何幫助。

不確定date-fns可能有什么問題

但是你可以在沒有庫的情況下實現這一點:

 const difference = new Date("Sun Jul 19 2020 15:13:20 GMT-0600 (Mountain Daylight Time)") - new Date("Fri Jul 17 2020 21:15:40 GMT-0600 (Mountain Daylight Time)") // the difference is in miliseconds. // divide it by miliseconds in a day to get the difference in days const differenceInDays = Math.round(Math.abs((difference) / (24 * 60 * 60 * 1000))) console.log(differenceInDays)

暫無
暫無

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

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