How to insert today's date into a URL?

后端 未结 6 485
陌清茗
陌清茗 2020-12-11 23:11

I have a url that change every day based on today\'s date, for example:

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


        
相关标签:
6条回答
  • 2020-12-11 23:18

    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.

    0 讨论(0)
  • 2020-12-11 23:22

    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 :

    //<![CDATA[ 
    window.onload=function(){
    
    ....javascript code here....
    
    }//]]>
    
    0 讨论(0)
  • 2020-12-11 23:27

    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>

    0 讨论(0)
  • 2020-12-11 23:32
     var date = new Date().toDateString("yyyyMMdd");
    

    then paste the date in building the URL

    url = "http://blahblahblaj.com/"+date

    0 讨论(0)
  • 2020-12-11 23:38

    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);
    
    0 讨论(0)
  • 2020-12-11 23:43

    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?

    <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>
    
    0 讨论(0)
提交回复
热议问题