gpt4 book ai didi

javascript - 从 unix 时间戳到日期时间

转载 作者:IT王子 更新时间:2023-10-29 03:21:30 26 4
gpt4 key购买 nike

我有像 /Date(1370001284000+0200)/ 这样的时间戳。我想这是一个 unix 日期,不是吗?我如何将其转换为这样的日期:31.05.2013 13:54:44

我试过了 THIS 1370001284 的转换器,它给出了正确的日期。所以它以秒为单位。

但我仍然得到错误的日期:

var substring = unix_timestamp.replace("/Date(", "");
substring = substring.replace("000+0200)/", "");
var date = new Date();
date.setSeconds(substring);
return date;

最佳答案

Note my use of t.format comes from using Moment.js, it is not part of JavaScript's standard Date prototype.

Unix 时间戳是自 1970-01-01 00:00:00 UTC 以来的秒数。

+0200 的存在意味着数字字符串不是 Unix 时间戳,因为它包含时区调整信息。您需要单独处理。

如果您的时间戳字符串以毫秒为单位,那么您可以使用毫秒构造函数和Moment.js。将日期格式化为字符串:

var t = new Date( 1370001284000 );
var formatted = moment(t).format("dd.mm.yyyy hh:MM:ss");

如果您的时间戳字符串以秒为单位,则使用 setSeconds:

var t = new Date();
t.setSeconds( 1370001284 );
var formatted = moment(t).format("dd.mm.yyyy hh:MM:ss");

关于javascript - 从 unix 时间戳到日期时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16978331/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com