HTML 显示当前日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32540044/
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-29 18:13:13  来源:igfitidea点击:

HTML Display Current date

htmldate

提问by Miqro

I am using website builder called 'clickfunnels', and they don't support feature that would allow me to display current date. But, I can add custom html to it.

我正在使用名为“clickfunnels”的网站构建器,但它们不支持允许我显示当前日期的功能。但是,我可以向其中添加自定义 html。

I was wondering if anyone knows how to show on website current date in format: dd/mm/yyyy

我想知道是否有人知道如何在网站上以格式显示当前日期:dd/mm/yyyy

Currently I've tried this:

目前我已经尝试过这个:

<p id="date"></p>
<script>
document.getElementById("date").innerHTML = Date();
</script>

And this works, but it displays date likes this:

这有效,但它显示的日期是这样的:

Sat Sep 12 2015 16:40:10 GMT+0200 (Timezone.... )

2015 年 9 月 12 日星期六 16:40:10 GMT+0200(时区....)

回答by Lance

Here's one way. You have to get the individual components from the date object (day, month & year) and then build and format the string however you wish.

这是一种方法。您必须从日期对象(日、月和年)中获取各个组件,然后根据需要构建和格式化字符串。

n =  new Date();
y = n.getFullYear();
m = n.getMonth() + 1;
d = n.getDate();
document.getElementById("date").innerHTML = m + "/" + d + "/" + y;
<p id="date"></p>

回答by maowtm

Use Date::toLocaleDateString.

使用Date::toLocaleDateString.

new Date().toLocaleDateString()
= "9/13/2015"

You don't need to set innerHTML, just by writing

您不需要设置innerHTML,只需编写

<p>
<script> document.write(new Date().toLocaleDateString()); </script>
</p>

will work.

将工作。

supportness

支持

P.S.

聚苯乙烯

new Date().toDateString()
= "Sun Sep 13 2015"

回答by Orlando Reyes

I prefer to use

我更喜欢使用

<input type='date' id='hasta' value='<?php echo date('Y-m-d');?>'>

that works well

效果很好

回答by Mihai Matei

var currentDate  = new Date(),
    currentDay   = currentDate.getDate() < 10 
                 ? '0' + currentDate.getDate() 
                 : currentDate.getDate(),
    currentMonth = currentDate.getMonth() < 9 
                 ? '0' + (currentDate.getMonth() + 1) 
                 : (currentDate.getMonth() + 1);

document.getElementById("date").innerHTML = currentDay + '/' + currentMonth + '/' +  currentDate.getFullYear();

You can read more about Date object

您可以阅读有关Date 对象的更多信息

回答by RAKESH BADGUJAR

This helped me:

这帮助了我:

<p>Date/Time: <span id="datetime"></span></p><script>var dt = new Date();
document.getElementById("datetime").innerHTML=dt.toLocaleString();</script>    

回答by Masood Ahmad

  <script >
window.onload = setInterval(clock,1000);
function clock()
{
    var d = new Date();
    var date = d.getDate();
    var year = d.getFullYear();
    var month = d.getMonth();
    var monthArr = ["January", "February","March", "April", "May", "June", "July", "August", "September", "October", "November","December"];
    month = monthArr[month];
    document.getElementById("date").innerHTML=date+" "+month+", "+year;
}