gpt4 book ai didi

javascript - 如何在 Python 中使用 Javascript +Date() 数字时间戳?

转载 作者:行者123 更新时间:2023-11-29 19:32:08 24 4
gpt4 key购买 nike

我有一个 JavaScript 日期字符串,我想将其转换为可理解的形式。例如。

new Date(1415988000000)

给出输出

Fri Nov 14 2014 23:30:00 GMT+0530 (IST)

我想在 python 中执行此操作。我不想使用 PyV8。还有其他选择吗?

最佳答案

您不需要在 Python 中编写 Javascript 来转换时间戳。

以下在纯 python 中工作。

注意:

  • JS 日期格式是自 1970 年 1 月 1 日 UTC 以来的毫秒数。
  • 奇怪的是,time.time() 的 python 格式是自 1970 年 1 月 1 日 UTC 以来的秒数。

此标准有时称为 Unix time

首先,对于毫秒时间到秒时间的转换,需要将1415988000000除以1000,得到1415988000

然后你可以像这样使用datetime库:

import datetime
d = datetime.datetime.fromtimestamp(1415988000)
print d

获取:

2014-11-14 13:00:00

此打印似乎已将 d 转换为我的 TZ,即 UTC-5。

因此 UTC 时间为 18:00。

这解释了稍后的时间,23:30,您在 JS 中收到 IST 或 UTC+5:30 中的相同戳记

关于javascript - 如何在 Python 中使用 Javascript +Date() 数字时间戳?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26885123/

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