JavaScript Date.now到可读格式

rkue9o1l  于 11个月前  发布在  Java
关注(0)|答案(5)|浏览(97)
Date.now();

字符串
我想把时间戳转换成可读格式
例如“Fri Nov 17 2017 19:15:15 GMT+0800(马来半岛标准时间)”

ncgqoxb0

ncgqoxb01#

var date = new Date();
console.log(date.toLocaleDateString());
console.log(date.toDateString());
console.log(date.toLocaleString());
console.log(date.toUTCString());
console.log(date.toISOString());

字符串
从这里选择您的方法:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

mcdcgff0

mcdcgff02#

你可以做下面这些陈述

let newDate = new Date();
console.log(`${newDate.toDateString()} ${newDate.toTimeString()}`);

字符串

ctehm74n

ctehm74n3#

从上面的答案中,我们也可以用一行来做。如下所示,

var dateString = new Date().toDateString();
console.log(dateString, '\n');
var dateString = new Date().toLocaleDateString();
console.log(dateString, '\n');
var dateString = new Date().toGMTString();
console.log(dateString, '\n');

字符串

o4hqfura

o4hqfura4#

您可以将从Date.now()获得的值传递给new Date(),这将以可读格式给予时间。
举例来说:

let timestamp = Date.now();
console.log(new Date(timestamp))

字符串

xdyibdwo

xdyibdwo5#

new Date();

document.getElementById("demo").innerHTML = Date();

个字符

相关问题