試圖通過這樣做,只顯示日期:如何在js中顯示UTC日期字符串的日期和時間?
let myDate="2015-03-08T04:49:49.431Z";
console.log(new Date(myDate).toUTCString('dd-mm-yyyy'));
仍返回的時候,我如何才能返回日期?
試圖通過這樣做,只顯示日期:如何在js中顯示UTC日期字符串的日期和時間?
let myDate="2015-03-08T04:49:49.431Z";
console.log(new Date(myDate).toUTCString('dd-mm-yyyy'));
仍返回的時候,我如何才能返回日期?
var myDate="2015-03-08T04:49:49.431Z";
new Date(myDate).toLocaleDateString(); // "3/8/2015"
new Date(myDate).toDateString(); // "Sun Mar 08 2015"
你可以在這裏查看所有其他方法:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
但是,這不會給你** dd-mm-yyyy **格式。 – dferenc
你可以這樣做:
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()
不接受任何參數。
你試過https://stackoverflow.com/questions/1531093/how-do-i-get-the-current-date-in-javascript – weirdpanda
可能重複的[如何在JavaScript中獲取當前日期? ](https://stackoverflow.com/questions/1531093/how-do-i-get-the-current-date-in-javascript) – weirdpanda