gpt4 book ai didi

javascript倒计时与年,月,日

转载 作者:行者123 更新时间:2023-11-29 18:28:05 30 4
gpt4 key购买 nike

我正在尝试使用 javascript 进行倒计时。但是,我的倒计时只能计算天数、小时数、分钟数和秒数。我还想显示年份和月份。

以下是我的代码:

<script type="text/javascript">

today = new Date();

BigDay = new Date("December 25, 2016");
msPerDay = 24 * 60 * 60 * 1000;
timeLeft = (BigDay.getTime() - today.getTime());
e_daysLeft = timeLeft / msPerDay;
daysLeft = Math.floor(e_daysLeft);
e_hrsLeft = (e_daysLeft - daysLeft) * 24;
hrsLeft = Math.floor(e_hrsLeft);
minsLeft = Math.floor((e_hrsLeft - hrsLeft) * 60);
// $("#countdown").append("There are only<BR> <H4>" + daysLeft + " days " + hrsLeft + " hours and " + minsLeft + " minutes left </H4> Until December 25th 2020<P>");
document.write(daysLeft + " days " + hrsLeft + " hours" + minsLeft + " minutes");

</script>

我想输出:

x Years, y Months, z Days left.

最佳答案

您走在正确的轨道上。如果天数超过 365,只需按照相同的方法将天数除以 365,然后对 daysLeft 取模 365。

(虽然它不会很精确,但它很简单,如果你不需要精确的单位,你可以使用它。)

var today = new Date();

var BigDay = new Date("December 25, 2018");
var msPerDay = 24 * 60 * 60 * 1000;
var timeLeft = (BigDay.getTime() - today.getTime());
var e_daysLeft = timeLeft / msPerDay;
var daysLeft = Math.floor(e_daysLeft);
var yearsLeft = 0;
if (daysLeft > 365) {
yearsLeft = Math.floor(daysLeft / 365);
daysLeft = daysLeft % 365;
}
var e_hrsLeft = (e_daysLeft - daysLeft) * 24;
var hrsLeft = Math.floor(e_hrsLeft);
var minsLeft = Math.floor((e_hrsLeft - hrsLeft) * 60);
document.write(yearsLeft + " years " + daysLeft + " days " + hrsLeft + " hours " + minsLeft + " minutes");

关于javascript倒计时与年,月,日,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11112497/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com