0

I have a JQuery function which is supposed to automatically download a date stamped XML file from a URL from Monday of the previous week each week.

I've coded the following JQuery and HTML:

$(function() {
  $('a[data-auto-download]').each(function(){
    var $this = $(this);
    setTimeout(function() {
      window.location = $this.attr('href');
    }, 2000);
  });
});

The corresponding HTML which calls the function is the following:

<p>The download should start shortly. If it doesn't, click
<a data-auto-download href="https://download.microsoft.com/download/0/1/8/018E208D-54F8-44CD-AA26-CD7BC9524A8C/PublicIPs_20190513.xml">here</a>.</p>

Example of the full URL is:

https://download.microsoft.com/download/0/1/8/018E208D-54F8-44CD-AA26-CD7BC9524A8C/PublicIPs_20190513.xml

The following URL portion is always static each week:

https://download.microsoft.com/download/0/1/8/018E208D-54F8-44CD-AA26-CD7BC9524A8C/PublicIPs_

I would like to parametrize just the target filename portion of the URL to download each week (immediately following the underscore '_'), for example...

20190513.xml

… such that it always returns Monday of the previous week automatically.

How do I complete my JQuery function so that the XML date stamp always reflects the Monday of the previous week as of the current date?

For example, if we're the 21st of May 2019 (or 22nd, 23rd, or 24th) for that matter), I would like the filename to reflect **/PublicIPs_20190513.xml.

In another example, if we're now the 27th of May 2019, the file datestamp should now reflect the following:

PublicIPs_20190520.xml

Any guidance would be very much appreciated!

Thanks very much in advance, Shawn

Shawn_M
  • 35
  • 7
  • Possible duplicate of [How to get first and last day of the week in JavaScript](https://stackoverflow.com/questions/5210376/how-to-get-first-and-last-day-of-the-week-in-javascript) – freedomn-m May 21 '19 at 14:25
  • If you want "previous week" use the above then add -7 days. Otherwise it's just reformatting the date from yyyymmdd format into a date – freedomn-m May 21 '19 at 14:26
  • @freedomn-m, thanks very much! I'm very new to JQuery, and I don't know where I would need to add my variables. Do I add them inside my current function or would they be global variables outside of my function (before or after)? Thereafter, how would I pass my variable to my HTML URL portion? – Shawn_M May 21 '19 at 14:42

0 Answers0