gpt4 book ai didi

javascript - IE 没有正确比较日期字符串

转载 作者:行者123 更新时间:2023-11-29 21:54:17 26 4
gpt4 key购买 nike

我有这个代码,我只想在特定日期出现。这段代码在 Firefox、Chrome 甚至 safari 中运行良好。但是,该代码不适用于 IE。我不知道为什么。

我发现 .toLocaleString() 在 IE 中用空格而不是逗号分隔它们。

  function givingTuesdayCode(){
var isIE = /*@cc_on!@*/false || !!document.documentMode;
var now = calcTime(-6);
var splitnow = "";
if ( isIE ){ splitnow = now.split(" "); }
else{ splitnow = now.split(","); }

if (splitnow[0] == "12/2/2014"){

$('.introrotation').html("<img style='width:100%; height:auto' class='givingTuesday' src='/graphics/PoliticGov_620x265.jpg' alt='Give to us'/> <a href='http://IllinoisState.edu/GivingTuesday' class='GiveButtonLink'>Giving Tuesday: Join us!</a> <p style='color:black; position:relative; top:-85px; margin:10px'>Black Friday and Cyber Monday have come and gone. Today, join your fellow Redbirds and make a gift that matters. Give today at <a href='http://IllinoisState.edu/GivingTuesday' class='GiveLink'>IllinoisState.edu/GivingTuesday</a></p>");
$('.introrotation').css({'height': '265px'
});
$('.toggleButton').css({'display': 'none'
});
}

function calcTime(offset){
var date = new Date();
var utc = date.getTime()+(360*60000);
var nd = new Date(utc+(3600000*offset));
return nd.toLocaleString();
}

最佳答案

与其尝试将日期与特定字符串匹配(这可能会在其他语言环境中中断),不如直接比较特定日期:

// Reset the hours, minutes, etc. so that comparison works
var today = (new Date()).setHours(0, 0, 0, 0);

// Month is zero-indexed (i.e. 0 = Jan, 11 = Dec)
var specificDate = (new Date(2014, 11, 2)).getTime();

if (today === specificDate) {
$('.introrotation').html("<img style='width:100%; height:auto' class='givingTuesday' src='/graphics/PoliticGov_620x265.jpg' alt='Give to us'/> <a href='http://IllinoisState.edu/GivingTuesday' class='GiveButtonLink'>Giving Tuesday: Join us!</a> <p style='color:black; position:relative; top:-85px; margin:10px'>Black Friday and Cyber Monday have come and gone. Today, join your fellow Redbirds and make a gift that matters. Give today at <a href='http://IllinoisState.edu/GivingTuesday' class='GiveLink'>IllinoisState.edu/GivingTuesday</a></p>");
$('.introrotation').css({'height': '265px'});
$('.toggleButton').css({'display': 'none'});
}

参见 Compare two dates with JavaScript有关比较日期的更多信息。

理想情况下,您应该在服务器端执行此操作,而不是在客户端执行此操作,这样您的消息仍会在禁用 JavaScript 的浏览器中显示。

关于javascript - IE 没有正确比较日期字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27300739/

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