gpt4 book ai didi

javascript - 如何将今天的日期插入 URL?

转载 作者:行者123 更新时间:2023-11-30 00:34:43 25 4
gpt4 key购买 nike

我有一个 url,它根据今天的日期每天都在变化,例如:

http://www.newspaper.com/edition/20141227.html

其中 20141227 的格式为 YYYYMMDD。

我可以使用 JavaScript 包含日期吗?如果可能,我该怎么做?

最佳答案

我认为以下步骤将帮助您实现您正在寻找的功能

1.将今天的日期或任何日期转换为预期格式,在您的情况下为“YYYYMMDD”。

2.然后将其附加到您的网址。

有关详细信息,请查看代码片段。请注意,您只需将鼠标悬停在 URL 上即可知道它指向什么。

Date.prototype.toMyString = function () {
//If month/day is single digit value add perfix as 0
function AddZero(obj) {
obj = obj + '';
if (obj.length == 1)
obj = "0" + obj
return obj;
}

var output = "";
output += this.getFullYear();
output += AddZero(this.getMonth()+1);
output += AddZero(this.getDate());

return output;
}

var d = new Date();

var link = document.getElementById("link");

link.setAttribute("href","/yourchoiceofURL?t="+d.toMyString());
<ul>
<li><a id="link" href="#">Any URL</a></li>
</ul>

关于javascript - 如何将今天的日期插入 URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27728219/

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