2017-10-15 56 views
2

我正在做一個不和諧的機器人,並希望它能夠說出它開始的時間,但是當我使用Date();它有一個完全不同的時間比我的計算機的本地時間,這使得它真的很難分辨它的時候返回其實開始了。如何取得Date();在JavaScript中顯示時間是正確的?

我正在使用的代碼:

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

我得到的時間:

2017-10-15T10:37:14.912Z 

回答

3

你可以試試這個:

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

1

,你可以試試下面的代碼

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瞭解更多信息

1

考慮使用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/

相關問題