javascript 如何将今天的日期插入到 URL 中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27728219/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to insert today's date into a URL?
提问by PUSTAKAKORAN.COM
I have a url that change every day based on today's date, for example:
我有一个根据今天的日期每天都在变化的网址,例如:
http://www.newspaper.com/edition/20141227.html
where 20141227 is in the format YYYYMMDD.
其中 20141227 的格式为 YYYYMMDD。
Can I include the date using JavaScript? If possible, how would I do that?
我可以使用 JavaScript 包含日期吗?如果可能,我该怎么做?
采纳答案by Dnyanesh
I think following steps will help you to achieve the functionality your are looking for
我认为以下步骤将帮助您实现您正在寻找的功能
1.Convert the today's date or any date to intended format that is "YYYYMMDD" in your case.
1.在您的情况下,将今天的日期或任何日期转换为“YYYYMMDD”的预期格式。
2.Then append it to your URL.
2.然后将其附加到您的 URL。
Please look into code snippet for details. Note you just need to hover over URL to know what it is pointing to.
请查看代码片段以获取详细信息。请注意,您只需将鼠标悬停在 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>
回答by Adi
Here's a simpler method that works
这是一个更简单的方法
<script>
var link = document.getElementById('link'); // ref. to your anchor tag
var d = new Date,
date = d.getDate(),
month = d.getMonth()+1, // Months in JavaScript are 0 indexed
year = d.getFullYear();
if(date < 10) date = ("0" + date);
if(month < 10) month = ("0" + month);
link.href = ("STATIC_URL/" + year + month + date);//Concatenating three numbers, kind of a hack
</script>
This is as simple as it gets.
这很简单。
回答by HeiN
You can try the below code. Hope this helps.
你可以试试下面的代码。希望这可以帮助。
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();
if(dd<10){dd='0'+dd};
if(mm<10){mm='0'+mm};
today = yyyy+mm+dd;
var new_url=document.URL+"/"+today+".html";
console.log(new_url);
回答by PUSTAKAKORAN.COM
Thanks for all the answers colleagues. For Dnyanesh, I tried the code in http://jsfiddle.net/can work well. I try to enter into an html page like this, why can not run perfectly. Where is the mistake?
感谢所有回答的同事。对于 Dnyanesh,我尝试了http://jsfiddle.net/ 中的代码可以很好地工作。我尝试进入这样的html页面,为什么不能完美运行。错误在哪里?
<html>
<head>
<script type='text/javascript'>
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","http://www.pressdisplay.com/pressdisplay/pageview.aspx?issue=1245"+d.toMyString()+"00000000001001");
</script>
</head>
<body>
<a id="link" href="#">Any URL</a>
</body>
</html>
回答by PUSTAKAKORAN.COM
Thank you for all, all of the recommended code runs fine. If you want to put into the HTML code , add the following code to be loaded in the browser :
谢谢大家,所有推荐的代码都运行良好。如果要放入HTML代码,添加如下代码在浏览器中加载:
//<![CDATA[
window.onload=function(){
....javascript code here....
}//]]>
回答by MrE
var date = new Date().toDateString("yyyyMMdd");
then paste the date in building the URL
然后在构建 URL 时粘贴日期
url = "http://blahblahblaj.com/"+date
url = " http://blahblahblaj.com/"+日期