2

I am trying to link to a website that changes daily...

The URL is http://www.lwcb.ca/waterflowdata.html and the link is "Winnipeg River in Manitoba". The website updates itself by adding the current date to the end of the filename. For example if today is December 29th, then it will link to http://www.lwcb.ca/pdf/WinnipegRiverManitobaGraph2015.12.29.pdf.

I need this file to be linkable from another website and cannot seem to find any code out there that will do anything similar. Any ideas?

fedorqui
  • 275,237
  • 103
  • 548
  • 598

5 Answers5

1

You can generate today's date like this:

var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();

Then, process the input to generate the desired string:

today = yyyy + '.' + mm + '.' dd;
var url = "http://www.lwcb.ca/pdf/WinnipegRiverManitobaGraph" + today + ".pdf";

Finally it is a matter of creating a link that uses this value.


In case you want to use today's date just if it is after noon, say:

var today = new Date();
if (today.getHours() < 12) {
    date.setDate(date.getDate() - 1);
    var today = new Date();
}

See an example:

var today = new Date();
if (today.getHours() < 12) {
    today = (function(d){ d.setDate(d.getDate()-1); return d})(new Date)
}
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();
var mydate = yyyy + '.' + mm + '.' + dd;
var url = "http://www.lwcb.ca/pdf/WinnipegRiverManitobaGraph" + mydate + ".pdf";
document.write(url);

And see a full working example:

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){

var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();
today = yyyy + '.' + mm + '.' + dd;
var url = "http://www.lwcb.ca/pdf/WinnipegRiverManitobaGraph" + today + ".pdf";

$("a#url").attr("href", url)
});
</script>
</head>
<body>


<a href="a" id="url">Link to my pdf</a>
<a href="www.google.com" id="blabla">Link to google</a>
Community
  • 1
  • 1
fedorqui
  • 275,237
  • 103
  • 548
  • 598
  • This works perfectly but I was wondering how the actual URL variable would be implemented into my tag? – James Rogowy Dec 30 '15 at 13:52
  • @JamesRogowy I just updated with a full working example that changes the `href` value on loading the page. – fedorqui Dec 30 '15 at 14:11
  • One problem with the way the code works as it is now. For every single link on the page it now goes to the URL variable. I only want it to link to it on a single within the page. – James Rogowy Dec 30 '15 at 14:32
  • @JamesRogowy yep, I had thought about it but did not implement. It is just a matter of changing `$('a')` to `$('a#url')`. That is, to the id of this link. See updated example. – fedorqui Dec 30 '15 at 14:35
  • Ok now let's say I am ambitious and I wanted to add a second link that works on the same principle but with a different name: var url2 = "http://www.lwcb.ca/pdf/LowerWinnipegRiverTributariesGraph12" how would I link that to a new tag? – James Rogowy Dec 30 '15 at 15:19
  • @JamesRogowy This is getting outside the scope of this specific question. You should try it yourself and ask a new question if you do not manage to do it. – fedorqui Dec 30 '15 at 15:22
1

Get the current date then insert it in the path. If you want to store in the database or something then use a server-side language like PHP/ASP.

//JS

var date = new Date();
var today = date.getDate();
var month = date.getMonth();
var year = date.getFullYear();
var date = year+"."+(month+1)+"."+today;
var path = "http://www.lwcb.ca/pdf/WinnipegRiverManitobaGraph/"+date+".pdf";
console.log(path);
Jordan Davis
  • 1,485
  • 7
  • 21
  • 40
1

Try following.

var d=new Date()
var url=link+d.getFullYear()+'.'+(d.getMonth() + 1) + '.' + d.getDate() 
T J
  • 42,762
  • 13
  • 83
  • 138
Piyush Aghera
  • 965
  • 7
  • 13
0

It's a tougher question than it may seem.

All the answers so far suggest a JavaScript solution for date generation, but that's not a solution that covers all the scenarios.

The document you will be linking to appears to be a daily report, I assume generated based on the data gathered. Problems start when the person accessing your website would have an incorrect system date or if they are simply located in different timezone.

This results in a situation where you want to link to the current, latest report, but you might be linking to non-existant or old report instead.

The best way to resolve that would be to have the server where the file is hosted be responsible for providing the latest report. A simple script checking for the latest generated report and redirecting to it would suffice. This would additionally solve the problem when a report fails to generate, in which case user would get the last valid data instead.

Alternatively, you can either generate the link on your server, in a known timezone (and account for the difference) or worst case scenario - account for the timezone on client side in Javascript - but that wouldn't cover all scenarios anyway.

The exact implementation details depend on the server languages you have available.

Ethnar
  • 657
  • 4
  • 10
-1

concatenate Date to the link. it will enable you keep abreast with the link