如何取得Date();在JavaScript中显示时间是正确的?

问题描述:

我正在做一个不和谐的机器人,并希望它能够说出它开始的时间,但是当我使用Date();它有一个完全不同的时间比我的计算机的本地时间,这使得它真的很难分辨它的时候返回其实开始了。如何取得Date();在JavaScript中显示时间是正确的?

我正在使用的代码:

var currentdate = new Date(); 
console.log(currentdate) 

我得到的时间:

2017-10-15T10:37:14.912Z 

你可以试试这个:

var d = new Date(); 
 
console.log(d.toLocaleTimeString()); 
 
console.log(d.toLocaleString()); 
 
console.log(d.toLocaleDateString());

,你可以试试下面的代码

var currentdate = new Date(); 
 
var datetime = "Date Time: " + currentdate.getDate() + "/" 
 
    + (currentdate.getMonth()+1) + "/" 
 
    + currentdate.getFullYear() + " @ " 
 
    + currentdate.getHours() + ":" 
 
    + currentdate.getMinutes() + ":" 
 
    + currentdate.getSeconds(); 
 
console.log(datetime)

Click here了解更多信息

考虑使用moment.js

它是时间相关的东西有很大的模块。如果您使用的是远程服务器,那么您看到的时间是GMT + 0。

随着时刻,你可以找到你的全球时区,并补充说,作为显示时间时的偏移量。还有便利的功能来控制显示格式。

例如在我的时区显示我只是用

moment().utcOffset(-4).format("dddd, MMMM Do YYYY, h:mm:ss a"); // "Sunday, February 14th 2010, 3:25:50 pm" 

https://momentjs.com/docs/#/manipulating/utc-offset/