将unix时间戳转换为本地时间

问题描述

如何将此乘以* 1000,以将unix时间戳转换为本地时间戳?

我写了这个表达式,但是出现了错误消息:

算术运算的左侧必须为“ any”类型, “数字”,“ bigint”或枚举类型。

 x: [new Date(this.unixtimearray[i].time)]*1000,

在unixtimearray中,您可以获得所有时间戳。 我期望dd / mm / yyyy hh:mm:ss作为返回值

解决方法

您需要将秒数乘以1000,然后将此值传递给Date()。阅读下面代码中的注释以获取更多信息

// Unix timestamp (sec)
const unixTimestamp = 1598949817;

// Get date (sec * 1000 = millisec)
const date = new Date(unixTimestamp * 1000);

// log
console.log(date);

// And you can format your output string like this
// If you need to add leading zeros in date and month
// you can use construction presented below
var dateFormat = ('0' + date.getUTCDate()).slice(-2) +"/"+ ('0' + (date.getUTCMonth()+1)).slice(-2) +"/"+ date.getUTCFullYear() + " " + date.getUTCHours() + ":" + date.getUTCMinutes() + ":" + date.getUTCSeconds();

// Log custom format
console.log(dateFormat);

,

尝试一下:

x: new Date(this.unixtimearray[i].time*1000)

如果this.unixtimearray[i].time是字符串:

x: new Date(parseInt(this.unixtimearray[i].time)*1000)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...