gpt4 book ai didi

javascript - 带日期的 JS If 语句

转载 作者:行者123 更新时间:2023-11-29 23:17:42 27 4
gpt4 key购买 nike

我想在我的电子商务网站上添加以下内容:“要在 9 月 9 日星期一之前收到?在 2 小时 24 分钟内订购”,方法是将 X 天(预计发货天数)添加到当前日期。为此,我在网上查找了以下代码:

<div style="text-align: center;font-weight: 400; font-size: 19px; color: #002f40;" id="clockbox"></div>

<script type="text/javascript">
var tday = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
var tmonth = ["Jan.", "Feb.", "Mar.", "Apr.", "May", "June", "July", "Aug.", "Sept.", "Oct.", "Nov.", "Dec."];

function GetClock() {
var n = 8; // Shipping time
var today = new Date();
var d = new Date(today.getFullYear(), today.getMonth(), today.getDate() + n)
var nday = d.getDay(),
nmonth = d.getMonth(),
ndate = d.getDate();

var clocktext = "<strong>Want it by <span style='color: #af3831;'>" + tday[nday] + ", " + tmonth[nmonth] + " " + [ndate] + " </span>?</strong>";
document.getElementById('clockbox').innerHTML = clocktext;
}

GetClock();
setInterval(GetClock, 1000);

但是,星期天不发货,我想排除这一天,改为隔天发货。我试着在这里添加这段代码:

var sunday = tday[nday];
document.getElementById('sundaytext').innerHTML = sunday;

if (sundaytext = "Sunday") {

var clocktext = "<strong>Want it by <span style='color: #af3831;'> Monday, " + tmonth[nmonth] + " " + [ndate+1] + " </span>?</strong>";
} else

var clocktext = "<strong>Want it by <span style='color: #af3831;'>" + tday[nday] + ", " + tmonth[nmonth] + " " + [ndate] + " </span>?</strong>";
document.getElementById('clockbox').innerHTML = clocktext;

它没有用,正如您所见,这种方法并不是最聪明的方法,但我对 JS 的了解非常有限,因此,我加入了这个论坛来寻求您的帮助。任何评论都非常感谢!谢谢。

最佳答案

一切都很好,除了变量名......它应该是 if (sunday == "Sunday") (或者你可以使用 if(tday[nday]=="Sunday") ) 但您使用了 if (sundaytext = "Sunday").. 您的代码中没有这样一个名为“sundaytext”的变量..

var sunday = tday[nday];
document.getElementById('sundaytext').innerHTML = sunday;

if (sunday == "Sunday") {
var clocktext = "<strong>Want it by <span style='color: #af3831;'>Monday, " + tmonth[nmonth] + " " + [ndate+1] + " </span>?</strong>";
}
else{
var clocktext = "<strong>Want it by <span style='color: #af3831;'>" + tday[nday] + ", " + tmonth[nmonth] + " " + [ndate] + " </span>?</strong>";
}
document.getElementById('clockbox').innerHTML = clocktext;

关于javascript - 带日期的 JS If 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52115837/

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