我有一个脚本,打印当前的日期和时间在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


当前回答

看看这个也许会对你有用

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

其他回答

基本JS(很好学习):我们使用Date()函数,并做所有我们需要显示日期和日期在我们的自定义格式。

var myDate = new Date(); let daysList = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']; let monthsList = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Aug', 'Oct', 'Nov', 'Dec']; let date = myDate.getDate(); let month = monthsList[myDate.getMonth()]; let year = myDate.getFullYear(); let day = daysList[myDate.getDay()]; let today = `${date} ${month} ${year}, ${day}`; let amOrPm; let twelveHours = function (){ if(myDate.getHours() > 12) { amOrPm = 'PM'; let twentyFourHourTime = myDate.getHours(); let conversion = twentyFourHourTime - 12; return `${conversion}` }else { amOrPm = 'AM'; return `${myDate.getHours()}`} }; let hours = twelveHours(); let minutes = myDate.getMinutes(); let currentTime = `${hours}:${minutes} ${amOrPm}`; console.log(today + ' ' + currentTime);


Node JS(快速简单):使用(npm安装日期和时间)安装npm页面,然后运行以下命令。

let nodeDate = require('date-and-time');
let now = nodeDate.format(new Date(), 'DD-MMMM-YYYY, hh:mm:ss a');
console.log(now);
var currentdate = new Date();

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

将. getday()方法更改为. getdate(),并将1添加到month,因为它从0开始计算月份。

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

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

这里有设计的空间

来获得你应该使用的时间和日期

    new Date().toLocaleString();

>> "09/08/2014, 2:35:56 AM"

只得到你应该使用的日期

    new Date().toLocaleDateString();

>> "09/08/2014"

只得到你该用的时间

    new Date().toLocaleTimeString();

>> "2:35:56 AM"

或者如果你只是想要格式为hh:mm而不是AM/PM的美式英语时间

    new Date().toLocaleTimeString('en-US', { hour12: false, 
                                             hour: "numeric", 
                                             minute: "numeric"});
>> "02:35"

或英式英语

    new Date().toLocaleTimeString('en-GB', { hour: "numeric", 
                                             minute: "numeric"});

>> "02:35"

点击这里阅读更多。

var datetime = new Date().toLocaleString().slice(0,9) +" "+new Date(new Date()).toString().split(' ')[4];
console.log(datetime);