2017-11-25 88 views
0

試圖通過這樣做,只顯示日期:如何在js中顯示UTC日期字符串的日期和時間?

let myDate="2015-03-08T04:49:49.431Z"; 
console.log(new Date(myDate).toUTCString('dd-mm-yyyy')); 

仍返回的時候,我如何才能返回日期?

+0

你試過https://stackoverflow.com/questions/1531093/how-do-i-get-the-current-date-in-javascript – weirdpanda

+0

可能重複的[如何在JavaScript中獲取當前日期? ](https://stackoverflow.com/questions/1531093/how-do-i-get-the-current-date-in-javascript) – weirdpanda

回答

0

你可以這樣做:

let myDate = "2015-03-08T04:49:49.431Z"; 
let date = new Date(myDate); 
let dateString = ("0" + date.getUTCDate()).slice(-2) + "-" + ("0" + (date.getUTCMonth()+1)).slice(-2) + "-" + date.getUTCFullYear(); 
console.log(dateString); 

.toUTCString()不接受任何參數。

相關問題