You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JavaScript中MongoDB带小数时间戳转日期异常问题

MongoDB时间戳在JavaScript中转换异常问题

问题详情

MongoDB数据库里有一个时间戳字段,格式示例如下:

1657479170.7300725
1657479170.7301126
1657479170.7301197
1657479170.9120467
1657479170.932398

用其他方式转换该时间戳能得到正确结果,比如第一个时间戳转换后为:

10.07.2022 21:52:50

但在JavaScript中转换时却得到错误的日期:

1970-01-20 06:24:39

我使用的转换代码

ConvH.forEach(conv => {
    conv.tracker.events.forEach(element => {
        console.log(parseFloat( parseFloat(element.timestamp.toFixed(4))),  moment(new Date( parseFloat( element.timestamp.toFixed(4)))).format("YYYY-MM-DD HH:mm:ss"));
        element.timestamp = new Date(element.timestamp).toLocaleString();
        
    })
});

注:使用new Date(element.timestamp).toLocaleString()也会得到相同错误结果 :/

内容的提问来源于stack exchange,提问作者MrPand

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.26 05:48:22