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