Jump to content

Dynamic Date In URL


Antioch Tech

Recommended Posts

My first time posting.  I hope this is the correct forum.

I need to create links to daily articles on certain web sites.  On some sites, the URL for the article that day contains the date.  So the article for today would be www.site.com/June/25.  How do I write code to put elements of today's date in the link?

Link to comment
Share on other sites

It is complicated. You should probably install a CMS such as Wordpress which does that for you.

If you're running on an Apache server the first step would be to use the mod_rewrite directives to control how the URL works. After that you need a server-side programming language to read the URL and choose which content to display based on the URL.

 

Link to comment
Share on other sites

Here's a start.  I would recommend making the drop-down a dynamic list for the last 30 (?) days
from the current date using the Date() object.

    <select id='Peanuts'>
      <option value=""> Select </option>
      <option value="Choose date"> Choose date </option>
      <option value="2020/06/30"> 2020/06/30 </option>
      <option value="2020/06/29"> 2020/06/29 </option>
      <option value="2020/06/28"> 2020/06/28 </option>
      <option value="2020/06/27"> 2020/06/27 </option>
      <option value="2020/06/26"> 2020/06/26 </option>
    </select>  
    <script>
    console.clear();
    function Cartoon() {
      document.location.href
       = `https://www.gocomics.com/peanuts/${document.getElementById('Peanuts').value}`;
    }
    function init() {
      document.getElementById('Peanuts').addEventListener('change', Cartoon ) 
    } init();
    </script>

 

Link to comment
Share on other sites

Thank you for the attempt, but this does not do what I need.  I need the link today to point to today's strip.  Tomorrow the link will point to tomorrow's strip.  Each day the link would point to the strip for that day.  Not  a list of dates to choose from, but the strip for today's date.

Link to comment
Share on other sites

22 hours ago, Antioch Tech said:

Thank you for the attempt, but this does not do what I need.  I need the link today to point to today's strip.  Tomorrow the link will point to tomorrow's strip.  Each day the link would point to the strip for that day.  Not  a list of dates to choose from, but the strip for today's date.

How many days are to be included into the past?

Would be difficult to point to tomorrow's strip until that day has been posted.

Link to comment
Share on other sites

Try this:

<!DOCTYPE html><html lang="en"><head><title> Test Page </title>
<meta charset="UTF-8">
<meta name="viewport" content="width-device-width,initial-scale=1.0, user-scalable=yes"/>
<!-- link rel="stylesheet" href="common.css" media="screen" -->
<style>
 #Peanuts { font-size: 2em; }
</style>
</head>
<body>
<a id="Peanuts" href="" target='_blank'>Todays's Peanut Strip</a>
<script>
function init() {
  var tday = new Date();
  var link = 'https://www.gocomics.com/peanuts/'
           + tday.getFullYear()+'/'+(tday.getMonth()+1)+'/'+tday.getDate();
  document.getElementById('Peanuts').href = link;
} init();
</script>
</body>
</html>

 

  • Like 1
Link to comment
Share on other sites

2 hours ago, dsonesuk said:

You will have problem with date values less than 10, see first example for fix

 

Which post has the problem?  I don't see it with my last suggested code. (Works OK using FF on 2020/7/3)

Link to comment
Share on other sites

It depends on whether the website chose to accept numbers without leading zeroes or not. It seems that GoComics is prepared for both kinds of numbers so it is not a problem in this scenario.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...