Javascript HTML 如何在网页中插入动态日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4904667/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 14:40:46  来源:igfitidea点击:

HTML how do I insert dynamic date in webpage

javascripthtmldatetextstatic

提问by mattgcon

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. how do I do that?

我有一个静态网页,没有任何动态变化。但是,客户希望将日期插入到页面内的文本中。日期将始终是当前日期加一天。我怎么做?

采纳答案by NakedBrunch

Use JavaScript and insert the date onLoad.

使用 JavaScript 并插入日期 onLoad。

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

在这里查看一个工作示例:http: //jsfiddle.net/xGDvp/

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

这将更新日期如下:2011 年 2 月 5 日

Your HTML:

你的 HTML:

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

Your Javascript

你的 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>

回答by Yasir Arsanukaev

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

Downvoters:请注意,在给出此答案添加了标签“javascript” 。

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

如果您的服务器支持,您可以使用服务器端包含 (SSI)。

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

例如,如果您的服务器是 Apache,您可以将以下元素放入 HTML 页面以输出当前日期 + 1 天:

<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:

假设今天是 2011-02-05,您将在浏览器的页面上有以下输出:

...
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.

要输出完整的工作日名称,您可以使用date -v +1d '+DATE: %A %d, %Y',它为您提供DATE: Sunday 06, 2011.



Further reading:

进一步阅读:

回答by user1480922

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

来源

回答by rhaag71

If your not opposed to PHP, then you could do something like this... Also Tizag.. http://www.tizag.com/phpT/phpdate.php

如果你不反对 PHP,那么你可以做这样的事情......还有 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); 
?>

Quick search for "php echo date + 1 day" produced that little diddy :) That could certainly be expanded upon to any extent just about.

快速搜索“php echo date + 1 day”产生了那个小老爹:) 这当然可以扩展到任何程度。

回答by Victor

You could use javascript to insert the date somewhere in the page:

您可以使用 javascript 在页面中的某处插入日期:

<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:

和 html:

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

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

在这里测试:http: //jsfiddle.net/9xWUT/

回答by Richard Nunes

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.

我推荐 XDate,因为它可以节省很多打字时间。另外,请不要使用innerHTML,这是一个坏习惯。我只是在网页上做了同样的事情,关键是在您正在更新的标签下使用内联 javascript,尽管您也可以使用“onload”。

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.

注意:我确实使用 innerHTML 来快速删除嵌套标签,而不是递归“删除所有子项”,因为它位于不同的库中,与本示例无关。

See http://arshaw.com/xdate/

http://arshaw.com/xdate/

回答by Muhammad Hamayoon

You can do it in my way this is below

你可以按照我的方式做这在下面

HTML:

HTML:

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

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

JavaScript:

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();

回答by Robin

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

你可以在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:

然后,您可以在文本中添加例如 span 标签,并使用 javascript 插入日期。如果您可以使用 jQuery,则可以执行以下操作:

html:

html:

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

javascript:

javascript:

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