3

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?

Antony
  • 14,900
  • 10
  • 46
  • 74
PUSTAKAKORAN.COM
  • 455
  • 1
  • 5
  • 18
  • 1
    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date should contain almost everything you need. – Amadan Jan 01 '15 at 04:16

6 Answers6

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

then paste the date in building the URL

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

MrE
  • 19,584
  • 12
  • 87
  • 105
  • 1
    according to W3S the Javascript Date object has a toDateStrng method: http://www.w3schools.com/jsref/jsref_obj_date.asp if you're really into the details, then use momentjs http://momentjs.com/docs/ – MrE Jan 01 '15 at 04:24
  • 1
    Sorry, said that wrong. The method is defined, but not with a parameter to specify a format. http://www.ecma-international.org/ecma-262/5.1/#sec-15.9.5.3 – Jonathan Lonowski Jan 01 '15 at 04:25
  • so why did i get a -1 again? – MrE Jan 01 '15 at 04:29
  • @MrE: probably because your answer returns `"Thu Jan 01 2015"`, not `"20150101"`. – Qantas 94 Heavy Jan 01 '15 at 04:30
  • 1
    [*Date.prototype.toDateString*](http://ecma-international.org/ecma-262/5.1/#sec-15.9.5.3) is implementation dependant, there is no set format that it will return for all browsers. It also doesn't take any parameters, you can't specify the format of the string it returns. – RobG Jan 01 '15 at 04:51
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>
Dnyanesh
  • 2,265
  • 3
  • 20
  • 17
0

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.

Adi
  • 5,560
  • 1
  • 24
  • 37
0

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);
HeiN
  • 503
  • 3
  • 17
0

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>
PUSTAKAKORAN.COM
  • 455
  • 1
  • 5
  • 18
0

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

}//]]>
PUSTAKAKORAN.COM
  • 455
  • 1
  • 5
  • 18