簡體   English   中英

如何在展望未來的地方倒計時,所以當我一年中的日期過去時,它應該展望下一年

[英]How to make countdown where it looks to the future, so when my date in the year is passed it should look to the next year

我正在倒計時,但現在它只會在日期還沒有過去的情況下給出回應。 但我希望如果日期已經過去,它會轉到下一年。 我怎么能那樣做? 我想我需要在日期的 const 中使用“if”,但我不知道該怎么做。

  const difference = +new Date(`01/01/${year}`) - +new Date();

你好西蒙,首先,我想告訴你(作為提示)你不需要轉換日期來計算它們之間的差異。 話雖如此,我建議您使用date-fns進行日期計算,使用此庫您將能夠使用endOfDay(date)類的方法。 如果您仍然不想使用任何外部庫,可以使用setHours方法:

 const now = new Date() const endOfDay = new Date(new Date(now.toString()).setHours(23, 59, 59, 999)) const isBeforeEndOfDay = now <= endOfDay console.log(isBeforeEndOfDay)

並且要獲得兩個日期之間的差異,您不需要計算它是否是一天的結束:

 // To set two dates to two variables const date1 = new Date("06/30/2019"); const date2 = new Date("07/30/2019"); // To calculate the time difference of two dates const Difference_In_Time = date2.getTime() - date1.getTime(); // To calculate the no. of minutes between two dates const Difference_In_Days = Difference_In_Time / (1000 * 3600 * 24); const Difference_In_Hours = Difference_In_Days * 24 console.log(Difference_In_Days, Difference_In_Hours) // [...]

暫無
暫無

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

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