0

I have a question that has been resolved here: How to insert today's date into a URL?

Now I have other problem. I have the image URL as follows:

http://cache3-img1.pressdisplay.com/pressdisplay/docserver/getimage.aspx?file=61072015010500000000001001&page=1&scale=44

and I input to HTML page as follows:

<img src="http://cache3-img1.pressdisplay.com/pressdisplay/docserver/getimage.aspx?file=61072015010500000000001001&page=1&scale=44" width="150"/>

...20150105... numbers in the URL is today's date .

How do I insert today's date code on the IMAGE URL using javascript like my previous problem .

Cœur
  • 37,241
  • 25
  • 195
  • 267
PUSTAKAKORAN.COM
  • 455
  • 1
  • 5
  • 18

1 Answers1

2

To set an image source, first you get a reference to it, imagine it has an ID of dateImage:

<img id="dateImage">

You can get it with jQuery like so:

var img = $("#dateImage");

Then to get the date string, which I believe you have as yyyymmdd, you could do it like so:

var now = new Date();
var month = now.getMonth()+1;
if(month < 10) month = "0" + month;
var day = now.getDate();
if(day < 10) day = "0" + day;
var dateStr = now.getFullYear() + month + day;

This gives you a string like 20140109. You have other stuff in your url, but imagine it is just:

myserver.com/image.aspx?stuff=1&date=

You could then do:

img.attr("src", "http://myserver.com/image.aspx?stuff=1&date=" + dateStr);

Does this make sense? The code making dateStr could be a bit tighter.

Raymond Camden
  • 10,661
  • 3
  • 34
  • 68