我试着使用luxon库来远离moment --将1615065599.426264时间戳转换为ISO。
根据在线Epoch变换器,这对应于
格林尼治时间:2021年3月6日(星期六)晚上9:19:59.426 您的时区:2021年3月6日(星期六)晚上10:19:59.426 GMT+01:00 亲属:3天前
删除小数部分会得到相同的结果。
使用luxon的代码
let timestamp = 1615065599.426264
console.log(luxon.DateTime.fromMillis(Math.trunc(timestamp)).toISO())
console.log(luxon.DateTime.fromMillis(timestamp).toISO())<script src="https://moment.github.io/luxon/global/luxon.min.js"></script>
这个结果是
1970-01-19T17:37:45.599+01:00
1970-01-19T17:37:45.599+01:00它可疑地接近Unix时代 (1970-01-01 : 00:00:00)。
我的错误在哪里?
发布于 2021-03-09 19:41:39
因此,“统一时间”计算自1970年1月1日以来的秒数(),而Luxon (以及大多数事物JavaScript)则期望一个具有毫秒分辨率的值。
将您的值乘以1000将产生预期的结果:
> let timestamp = 1615065599.426264
undefined
> new Date(timestamp).toJSON()
'1970-01-19T16:37:45.599Z'
> new Date(timestamp * 1000).toJSON()
'2021-03-06T21:19:59.426Z'发布于 2021-08-15 10:55:58
Luxon可以使用.fromSeconds()函数接受UNIX /秒。然后可以使用.toISO()函数输出ISO格式。
在您的具体示例中:
const { DateTime } = require('luxon')
//your other code here
const myDateTime = DateTime.fromSeconds(1615065599.426264)
const myDateTimeISO = myDateTime.toISO()
//outputs '2021-03-07T08:19:59.426+11:00'参考文献:https://moment.github.io/luxon/#/parsing?id=unix-timestamps
https://stackoverflow.com/questions/66553494
复制相似问题