Dynamic Hyperlink

Hi Guys/ Gals,

I am trying to create a link based on the current date however it doesnt seem to work.

Could someone please point me in the right direction.

Here is what i have so far..

<html>
  <head>
    <script type="text/javascript" language="JavaScript">
      function getDate()
      {
        var now = new Date();
        var month	= now.getMonth();
        var day = now.getDate();
        var year = now.getYear();
        if(year < 2000) { year = year + 1900; }
        var dateString = 'http://test.com/' + monthnumber + '-' + monthday + '-' + year + '.html';
        return dateString;
      } // function getCalendarDate()		
    </script>
  </head>
	
  <body>
     <a href="javascript:getDate()">Today</a>
  </body>
</html>

thanks in advance,

Rob.

You have to explicitly set the link rather than expecting it to be evaluated on the fly. There are JS libraries that implement sprintf also consider the ternary operator in this context eg month>10 ? month +1 : '0' + (month +1) for more legible filenames

<html>
  <head>
    <script type="text/javascript" language="JavaScript">
      function setLink(variableLink)
      {
        var now = new Date();
        var month    = now.getMonth();
        var day = now.getDate();
        var year = now.getYear();
        if(year < 2000) { year = year + 1900; }
        //var dateString = sprintf("http://test.com/%2d-%2d-%4d.html" ,
        //                         month, day,year);
        var dateString ='http://test.com/'
                                  + month + '-'
                                  + day   + '_'
                                  + year  + '.html';
        var anchor=document.getElementById(variableLink);
        anchor.href=dateString;
      }
    </script>
  </head>
    
  <body "onLoad=setLink('variableLink');">
     <a Id="variableLink" href="">Today</a>
  </body>
</html>
1 Like

Works like a charm, thanks alot :slight_smile: