1

I wanted to make a bookmark that uses today's date in the URL; in other words, when the bookmark is launched, the end of the URL would vary each day. So today's would end in .../2017/1/31 and tomorrow's would end in .../2017/2/1.

I thought it might be easiest to just make a barebones HTML page that includes an inline JavaScript to get current year, month, and date and append it to the main URL (which never changes). Does this make sense? Is there an easier way to accomplish this?

I'm okay with HTML elements, but kind of clueless about JavaScript; I literally copied a snippet from another stackoverflow answer that sounded decent and put it into my head tags as you can see below, and tried to adapt my URL into the ahref link:

<HTML>
<head>
    <script>var d=new Date();</script>
</head>

<body>
    <a href="http://wol.org?t="+d.getTime()>Continue</a>
</body>
</HTML>
Vemonus
  • 868
  • 1
  • 16
  • 28
Brandon
  • 13
  • 2
  • You can also refer to this post : http://stackoverflow.com/questions/27728219/how-to-insert-todays-date-into-a-url – jsanchezs Jan 31 '17 at 18:37

3 Answers3

1

The following will run without need for clicking any buttons:

<HTML>
<head>
    <script>
      Date.prototype.yyyymmdd = function() { //returns YYYY/MM/DD
        var mm = this.getMonth() + 1; // getMonth() is zero-based
        var dd = this.getDate();

        return [this.getFullYear(),
          (mm>9 ? '' : '0') + mm,
          (dd>9 ? '' : '0') + dd
         ].join('/');
      };
      var date = new Date();
      window.location.href = "your.url.com/" + date.yyyymmdd();
    </script>
</head>
<body>
</body>
</HTML>

Date function from this answer: https://stackoverflow.com/a/3067896/3803371

Note I usually don't condone modification of native prototypes, but I'm feeling lazy today.

Community
  • 1
  • 1
SethWhite
  • 1,891
  • 18
  • 24
1

You cannot use javascript expression outside script tag. So you cannot call d.getTime like this. Instead of you can do this:

<a id="c" href="">Continue</a>
<script>
(function() { // wait for window load
  var d=new Date();
  var a = document.getElementById("c");
  a.href = "http://wol.org?t="+d.getTime();
})();
</script>
Mike
  • 737
  • 1
  • 7
  • 11
0

There's a couple problems with your code. First, you're mixing HTML and JavaScript. JavaScript can only go between the <script> tags. Also, the script needs to go below your link you want to modify.

If you want to get the date in the form year/month/day you'll have to do some modification to the date string you get back from your Date object. What I do below is basically get the date string and split it by / into an array. I know the first index is the month, second is the day, and third gives me the year. I store each of those into a variable to use and rearrange later.

I then had to locate the <a> element using getElementById() and then I changed the href value using my date variables.

var dateString = new Date().toLocaleDateString();
var dateArray = dateString.split('/');

var month = dateArray[0];
var day = dateArray[1];
var year = dateArray[2];

var dateOrder = year + "/" + month + "/" + day;

console.log(dateOrder);

var a = document.getElementById('link');
a.href += dateOrder;
<a id="link" href="http://wol.org?t=">Continue</a>

<script>
 // Javascript from above goes here
</script>
spencer.sm
  • 19,173
  • 10
  • 77
  • 88