我有一个脚本,打印当前的日期和时间在JavaScript,但日期总是错误的。代码如下:

var currentdate = new Date();
var datetime = "Last Sync: " + currentdate.getDay() + "/" + currentdate.getMonth() 
+ "/" + currentdate.getFullYear() + " @ " 
+ currentdate.getHours() + ":" 
+ currentdate.getMinutes() + ":" + currentdate.getSeconds();

它应该打印18/04/2012 15:07:33和打印3/3/2012 15:07:33


当前回答

这个小代码很简单,在任何地方都可以工作。

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

这里有设计的空间

其他回答

对于真正的mysql样式输出使用下面的函数:2019/02/28 15:33:12

如果你点击下面的“运行代码片段”按钮 它将向您展示一个简单的实时数字时钟示例 演示程序将出现在代码片段的下面。

function getDateTime() { var now = new Date(); var year = now.getFullYear(); var month = now.getMonth()+1; var day = now.getDate(); var hour = now.getHours(); var minute = now.getMinutes(); var second = now.getSeconds(); if(month.toString().length == 1) { month = '0'+month; } if(day.toString().length == 1) { day = '0'+day; } if(hour.toString().length == 1) { hour = '0'+hour; } if(minute.toString().length == 1) { minute = '0'+minute; } if(second.toString().length == 1) { second = '0'+second; } var dateTime = year+'/'+month+'/'+day+' '+hour+':'+minute+':'+second; return dateTime; } // example usage: realtime clock setInterval(function(){ currentTime = getDateTime(); document.getElementById("digital-clock").innerHTML = currentTime; }, 1000); <div id="digital-clock"></div>

看看这个也许会对你有用

<script language="JavaScript">
var dayarray=new Array("Sunday","Monday",
 "Tuesday","Wednesday","Thursday","Friday","Saturday")

var montharray=new Array("January","February","March",
 "April","May","June","July","August","September",
 "October","November","December")

function getthedate(){
var mydate=new Date()
var year=mydate.getYear()
if (year < 1000)
year+=1900
var day=mydate.getDay()
var month=mydate.getMonth()
var daym=mydate.getDate()
if (daym<10)
daym="0"+daym
var hours=mydate.getHours()
var minutes=mydate.getMinutes()
var seconds=mydate.getSeconds()
var dn="AM"
if (hours>=12)
dn="PM"
if (hours>12){
hours=hours-12
}
if (hours==0)
hours=12
if (minutes<=9)
minutes="0"+minutes
if (seconds<=9)
seconds="0"+seconds
//change font size here
var cdate="<small><font color='000000' face='Arial'><b>"+dayarray[day]+", 
    "+montharray[month]+" "+daym+", "+year+" "+hours+":"
 +minutes+":"+seconds+" "+dn
    +"</b></font></small>"
if (document.all)
document.all.clock.innerHTML=cdate
else if (document.getElementById)
document.getElementById("clock").innerHTML=cdate
else
document.write(cdate)
}
 if (!document.all&&!document.getElementById)
  getthedate()
  function goforit(){
  if (document.all||document.getElementById)
 setInterval("getthedate()",1000)
}

 </script>

enter code here

 <span id="clock"></span>

这个小代码很简单,在任何地方都可以工作。

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

这里有设计的空间

. getday返回星期几。你需要用. getdate代替。 . getmonth返回0到11的值。您需要在结果中添加1以获得“人类”月份数。

Here is my work around clock full format with day, date, year and time and make Sure the date of your PC is set to the right date and if you are using PHP make sure in php.ini date.timezone= xx where xx your current timezone function startTime() { var today=new Date(); // 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 var suffixes = ['','st','nd','rd','th','th','th','th','th','th','th','th','th','th','th','th','th','th','th','th','th','st','nd','rd','th','th','th','th','th','th','th','st','nd','rd']; var weekday = new Array(7); weekday[0] = "Sunday"; weekday[1] = "Monday"; weekday[2] = "Tuesday"; weekday[3] = "Wednesday"; weekday[4] = "Thursday"; weekday[5] = "Friday"; weekday[6] = "Saturday"; var month = new Array(12); month[0] = "January"; month[1] = "February"; month[2] = "March"; month[3] = "April"; month[4] = "May"; month[5] = "June"; month[6] = "July"; month[7] = "August"; month[8] = "September"; month[9] = "October"; month[10] = "November"; month[11] = "December"; document.getElementById('txt').innerHTML=(weekday[today.getDay()] + ',' + " " + today.getDate()+'<sup>'+suffixes[today.getDate()]+'</sup>' + ' of' + " " + month[today.getMonth()] + " " + today.getFullYear() + ' Time Now ' + today.toLocaleTimeString()); t=setTimeout(function(){startTime()},500); } <style> sup { vertical-align: super; font-size: smaller; } </style> <html> <body onload="startTime()"> <div id="txt"></div> </body> </html>