HTML how do I insert dynamic date in webpage

前端 未结 8 2055
暖寄归人
暖寄归人 2021-01-05 14:40

I have a static webpage, nothing changes dynamically. However the client wants a date insert into text within the page. The date will always be the current daet plus one day

相关标签:
8条回答
  • 2021-01-05 15:19

    Downvoters: Note that the tag "javascript" was added after this answer was given.

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

    If your server is Apache, you could, for example, put the following element into your HTML page to output a current date + 1 day:

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

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

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

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


    Further reading:

    • article Server Side Includes at Wikipedia
    • Apache Tutorial: Introduction to Server Side Includes
    • date(1) manual page at FreeBSD.org
    0 讨论(0)
  • 2021-01-05 15:20

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

    Then you can add for example a span tag in your text, and insert the date with 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 讨论(0)
  • 2021-01-05 15:22

    You can do it in my way 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 讨论(0)
  • 2021-01-05 15:25
    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;
    }
    

    Source

    0 讨论(0)
  • 2021-01-05 15:25

    I recommend XDate because it will save a lot of typing. Also, please don't use innerHTML, it's such a bad habit. I just did the same on a web page and the key thing was to use inline javascript beneath the tag you are updating although you can use 'onload' as well.

    In the page I add the tag and put in some default info just because:

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

    With this below that:

    <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 do use innerHTML to quickly obliterate nested tags rather than a recursive "delete all children" because that's in a different library and not pertinent to this example.

    See http://arshaw.com/xdate/

    0 讨论(0)
  • 2021-01-05 15:28

    You could use javascript to insert the date somewhere in 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 the html:

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

    test it here: http://jsfiddle.net/9xWUT/

    0 讨论(0)
提交回复
热议问题