如何将今天的日期插入到URL中? [英] How to insert today's date into a URL?

查看:405
本文介绍了如何将今天的日期插入到URL中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个每天根据今天的日期更改的URL,例如:

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

其中20141227的格式YYYYMMDD。



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

解决方案

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



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



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



请查看代码段以获取详细信息。请注意,您只需将鼠标悬停在网址上即可知道它指向的内容。



  Date.prototype.toMyString = function(){//如果月/日是单位数值,则将perfix添加为0函数AddZero(obj) {obj = obj +; if(obj.length == 1)obj =0+ obj return obj; } var output =;输出+ = this.getFullYear();输出+ = AddZero(this.getMonth()+ 1);输出+ = AddZero(this.getDate());返回输出; } var d = new Date(); var link = document.getElementById(link); link.setAttribute(href,/ yourchoiceofURL?t =+ d.toMyString());  

 < ul> < li>< a id =linkhref =#>任何URL< / a>< / li>< / ul>  

/ div>


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.

Can I include the date using JavaScript? If possible, how would I do that?

解决方案

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.

2.Then append it to your URL.

Please look into code snippet for details. Note you just need to hover over URL to know what it is pointing to.

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>

这篇关于如何将今天的日期插入到URL中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆