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


当前回答

我善意的回答是使用这个小小的JS: https://github.com/rhroyston/clock-js

clock.now   --> 1462248501241
clock.time  --> 11:08 PM
clock.weekday   --> monday
clock.day   --> 2
clock.month --> may
clock.year  --> 2016
clock.since(1462245888784)  --> 44 minutes
clock.until(1462255888784)  --> 2 hours
clock.what.time(1462245888784)  --> 10:24 PM
clock.what.weekday(1461968554458)   --> friday
clock.what.day('14622458887 84')    --> 2
clock.what.month(1461968554458) --> april
clock.what.year('1461968554458')    --> 2016
clock.what.time()   --> 11:11 PM
clock.what.weekday('14619685abcd')  -->     clock.js error : expected unix timestamp as argument
clock.unit.seconds  --> 1000
clock.unit.minutes  --> 60000
clock.unit.hours    --> 3600000
clock.unit.days --> 86400000
clock.unit.weeks    --> 604800000
clock.unit.months   --> 2628002880
clock.unit.years    --> 31536000000

其他回答

我善意的回答是使用这个小小的JS: https://github.com/rhroyston/clock-js

clock.now   --> 1462248501241
clock.time  --> 11:08 PM
clock.weekday   --> monday
clock.day   --> 2
clock.month --> may
clock.year  --> 2016
clock.since(1462245888784)  --> 44 minutes
clock.until(1462255888784)  --> 2 hours
clock.what.time(1462245888784)  --> 10:24 PM
clock.what.weekday(1461968554458)   --> friday
clock.what.day('14622458887 84')    --> 2
clock.what.month(1461968554458) --> april
clock.what.year('1461968554458')    --> 2016
clock.what.time()   --> 11:11 PM
clock.what.weekday('14619685abcd')  -->     clock.js error : expected unix timestamp as argument
clock.unit.seconds  --> 1000
clock.unit.minutes  --> 60000
clock.unit.hours    --> 3600000
clock.unit.days --> 86400000
clock.unit.weeks    --> 604800000
clock.unit.months   --> 2628002880
clock.unit.years    --> 31536000000

基本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开始计算月份。

这个问题很古老,答案也很古老。现在我们可以使用moment.js来获取当前日期,而不是那些怪异的函数,这实际上非常简单。所有要做的就是在我们的项目中包括moment.js,并获得一个格式良好的日期,例如,通过:

moment().format("dddd, MMMM Do YYYY, h:mm:ss a");

我认为这使得在javascript中处理日期更容易。

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