我有一个 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/