javascript - 根据日期重定向到不同的链接

标签 javascript date redirect

我做了一些研究,但找不到我要找的东西。

基本上,我只想根据特定日期更改重定向。它用于降临节日历,因此用户将单击“查看我们的降临节日历”链接,根据日期(12 月 1 日、2 日、3 日……),他们将看到 12 月每一天的不同网页。

我认为这可能是简单的事情,例如:

<script language="JavaScript">
var currentDate = new Date().getDate();
if (currentDate = 2014,11,26)
  window.location = "http://www.yahoo.com";
else if (currentDate = 2014,11,27)
  window.location = "http://www.youtube.com";
else if (currentDate = 2014,10,28)
  window.location = "http://www.google.com";
</script>

(我会用我的实际链接替换 ​​Google/youtube/etc,但无论日期是什么,它只会转到第一个链接)
我尝试过不同的日期格式,包括 YYYY/MM/DD 和年/月/日/小时/秒/毫秒。

如果这很容易做到,而且我错过了一些明显的东西,我深表歉意。但你可以通过提问来学习...

最佳答案

您的位置部分很好。这是您的代码缺少的比较部分..您可以这样做

var date = new Date()  // construct a Date instance
           .toISOString()  // convert into ISO time
           .split('T')[0]; // this will result in "2013-11-26"
if(date == "your date in the format year-month-day here"){
   window.location.href = "your url here";
}

关于javascript - 根据日期重定向到不同的链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27151008/

相关文章:

javascript - 数据表 rows().ids() 返回未定义

python - 如何使用django F表达式仅过滤日期时间字段的日期

python - 在 numpy.where 中访问对象方法?

performance - Apache 重定向/重写最大值

php - 使用 PHP 进行 SEO URL 重定向

.htaccess - 如何进行从 https 到 https 的 301 重定向?

javascript - 在页面加载时打开一个弹出窗口并在其他任何地方单击时隐藏 + 在调整窗口大小时动态跟随其父级

javascript - 当您调用 .changePage() 时,JQuery Mobile 会向 div 添加什么

javascript - 如何在 Array 中查找使用 JSON.parse 产生错误的对象

javascript - 无法比较 JavaScript 中的当前日期和表单日期( Angular 形式)