Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript Date Refresh

gavin1f4

New Coder
Hi,

I have this code that displays current date and then also the current date plus 4 days. How do I get the plus 4 days date to refresh every second like the current time. When the time passes midnight and is a new day the plus 4 days date is not updating.

Thanks

HTML:
<!DOCTYPE html>
<html>

<script type="text/javascript">
function display_c(){
var refresh=1000; // Refresh rate in milli seconds
mytime=setTimeout('display_ct()',refresh)
}

function display_ct() {
var strcount
var x = new Date()
document.getElementById('ct').innerHTML = x;
tt=display_c();
}
</script>

<body onload=display_ct();>
<h3>Today is:</h3>
<span id='ct' ></span>


</body>
</html>

<!DOCTYPE html>
<html>
<body>

<h3>ELITT Day is:</h3>
<p id="date"></p>

<script>
Date.prototype.addDays = function(days) {
var dat = new Date(this.valueOf());
dat.setDate(dat.getDate() + days);
return dat;
}

var dat = new Date();
document.getElementById("date").innerHTML = (dat.addDays(4));


(dat.addDays(4))
</script>

</body>
</html>
 
Last edited by a moderator:
Hey @gavin1f4.
There are quite a few errors in your code.

There can only be one of the <html>, <head> and <body> elements in a single HTML document. You have two html's and body's.

Your display_c() function seems to be that you're trying to run the display_ct() function every second?
There's a simple way to do that using the setInterval() method.

In your first <body> opening tag, you have the onload attribute, onload=display_ct();.
Although not required, I strongly recommend wrapping attribute values in either single or double quotes.
The semicolon is also not required here.

Try running your code in the W3 Validator and fixing the errors.
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom