如何在js中显示UTC datetring的日期和时间?

时间:2017-11-25 23:29:15

标签: javascript

尝试通过执行此操作来显示日期:

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

仍然返回时间,我该如何才能返回日期?

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分: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()不接受任何论据。