javascript - javascript日期对象与python日期时间对象兼容吗

标签 javascript python django

例如,在 python 中,日期可以类似于:2020-06-19T11:32:16.548109Z, 有没有一种方法可以将其转换为 javascript Date 对象?

最佳答案

如果你的Python日期被序列化为ISO 8601日期格式,那么您只需将字符串传递到 JavaScript 中的 Date 构造函数中即可。小数部分的精度是非标准的,因为它是由语言决定的。

// 2020-06-19T11:32:16.548Z
new Date('2020-06-19T11:32:16.548109Z').toISOString()
// Fri, 19 Jun 2020 11:32:16 GMT
new Date('2020-06-19T11:32:16.548109Z').toUTCString()

注意:在Python中,时间戳纪元的精度精确到微秒。 JavaScript 在反序列化时会忽略这些,因为它使用毫秒纪元。

来自Wikipedia :

If necessary for a particular application, the standard supports the addition of a decimal fraction to the smallest time value in the representation.

查看这篇文章,它解释了不同语言如何处理 ISO 8601:

ISO 8601 and Nanosecond Precision Across Languages

关于javascript - javascript日期对象与python日期时间对象兼容吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62472323/

相关文章:

javascript - 为所有事件类型附加一个处理程序

javascript - Crossfilter 是否需要平面数据结构?

javascript - 图像显示为链接后的空白

python - 没有内置插件的选择排序 Python

python - Django - 使用 Cloudinary 清除上传的图像时出现 'TypeError: expected string or bytes-like object'

javascript - 在 OpenLayers 示例中调用 domtoimage.toPng() 时出现 "SecurityError: This operation is insecure"

javascript - 如何动态过滤 jQuery Mobile 选择表单

python - 检查用户输入的文件名的可用性

python - 为什么 list.append() 返回 None?

Django内联表单集添加另一个并删除