How to insert today's date into a URL?

2020-02-07 05:40发布

问题:

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?

回答1:

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>



回答2:

 var date = new Date().toDateString("yyyyMMdd");

then paste the date in building the URL

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



回答3:

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);


回答4:

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.



回答5:

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>


回答6:

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....

}//]]>