HTML how to add dynamic date to web page

I have a static web page, nothing changes dynamically. However, the client needs to insert the date into the text on the page. The date will always be the current daet plus one day. how to do it?

+6
javascript date html static text
source share
8 answers

Use JavaScript and paste the onLoad date.

Take a look at a working example: http://jsfiddle.net/xGDvp/

This will update the date as follows: February 5, 2011

Your HTML:

<span id="spanDate"></span> 

Your javascript

  <script type="text/javascript"> var months = ['January','February','March','April','May','June','July', 'August','September','October','November','December']; var tomorrow = new Date(); tomorrow.setTime(tomorrow.getTime() + (1000*3600*24)); document.getElementById("spanDate").innerHTML = months[tomorrow.getMonth()] + " " + tomorrow.getDate()+ ", " + tomorrow.getFullYear(); </script> 
+4
source share

Downvoters . Please note that a javascript tag has been added after this answer.

You can use Server Side Includes (SSI) if your server supports them.

If your server is Apache, you can, for example, put the following element on your HTML page to display the current date + 1 day:

 <pre> <!--#exec cmd="date -v +1d '+DATE: %Y-%m-%d'" --> </pre> 

Assuming today is 2011-02-05, you will have the following output on your page in your browser:

 ... DATE: 2011-02-06 ... 

To display the full name of the day of the week, you can use date -v +1d '+DATE: %A %d, %Y' , which gives you DATE: Sunday 06, 2011 .


Further reading:

+2
source share
 window.onload = initDate; function initDate() { var dayName = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday","Saturday"); var monName = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"); var now = new Date(); var dtString = dayName[now.getDay()] + ", " + monName[now.getMonth()] + " " + now.getDate(); document.getElementById("dtField"). innerHTML = dtString; } 

A source

+2
source share

If you are not against PHP, then you can do something like this ... Also Tizag .. http://www.tizag.com/phpT/phpdate.php

 <?php $todayPlusADay = mktime(0, 0, 0, date("m"), date("d")+1, date("y")); echo "Todays Date Plus One Day (Tomorrow) ".date("m/d/y", $todayPlusADay); ?> 

A quick search for "php echo date + 1 day" produced this little diddy :) This could certainly be expanded to some extent.

+1
source share

You can use javascript to insert the date somewhere on the page:

 <script type="text/javascript"> var newDate = new Date(); newDate.setDate(newDate.getDate() + 1); //insert it via jquery $('#displayDate').html((newDate.getMonth() + 1) + '/' + newDate.getDate() + '/' + newDate.getFullYear()); //or insert it via javascript document.getElementById('displayDate').innerHTML = (newDate.getMonth() + 1) + '/' + newDate.getDate() + '/' + newDate.getFullYear(); </script> 

and html:

 <span id="displayDate"></span> 

check here: http://jsfiddle.net/9xWUT/

+1
source share

I recommend XDate because it will save a lot of typing. Also, please do not use innerHTML, this is such a bad habit. I just did the same on the web page, and the key was to use the built-in javascript under the tag that you are updating, although you can also use "onload".

On the page, I add the tag and add some default data just because:

 <p>Today is <span id="todays_date" style="font-style: italic;">November 1, 2015</span></p> 

Moreover, below:

 <script type="text/javascript"> var today = new XDate(); document.getElementById("todays_date").innerHTML = ""; document.getElementById("todays_date").appendChild(document.createTextNode(today.toString("MMMM d, yyyy"))); </script> 

NB: I use innerHTML to quickly erase nested tags, rather than the recursive "delete all children" because it is in a different library and does not apply to this example.

See http://arshaw.com/xdate/

+1
source share

You can do it in javascript. http://www.tizag.com/javascriptT/javascriptdate.php

Then you can add, for example, a span tag to your text and insert the date using javascript. If you can use jQuery, you can do something like:

HTML:

 <p>Tomorrow: <span class="tomorrow"></span></p> 

JavaScript:

 $(function() { var date = new Date() $(".tomorrow").html(date.getDate() + 1) // you'll have to search how to format the date }); 
0
source share

You can do it in my opinion, this is below

HTML:

<asp: Label ID = "Label1" runat = "server" Text = ''>

JavaScript:

 <script language="javascript" type="text/javascript"> function date_time() { var dt = new Date(); document.getElementById('<%=Label1.ClientID%>').innerHTML = dt; setTimeout(function () { date_time(); }, 1000); } date_time(); 

0
source share

All Articles