python - 如何用 Python 解析这样的时间 '\/Date(-62135596800000)\/'

标签 python datetime time

如何使用 Python 解析这样的时间 '/Date(-62135596800000)/'?

我搜索了 Internet,但无法找到解决方案。

链接:http://211.137.19.74:8089/Ajax/GetStationDetailList?cityName=%E6%B2%88%E9%98%B3

时间点:“/日期(-62135596800000)/” 时间应该是2016:12:31:01:00

最佳答案

/Date(N)/ 通常是由 .NET 中内置的 WCF JSON 序列化程序序列化的日期格式。然后可以通过在 JavaScript 中调用 new Date(N) 来重建该值。

在 JavaScript 中运行它会显示为什么这个日期很可能是错误的:

new Date(-62135596800000)
// Date 0001-01-01T00:00:00.000Z

如您所见,您得到了 0001-01-01 00:00:00.00。这也恰好是 .NET 中空 DateTime 对象的默认值:

default(DateTime).ToString("u") // 0001-01-01 00:00:00Z
new DateTime(0).ToString("u") // 0001-01-01 00:00:00Z

所以,很抱歉这么说,但那台服务器刚刚坏了。


综上所述,一旦获得有效日期,就可以使用 datetime.fromtimestamp 在 Python 中解析数字或 datetime.utcfromtimestamp :

>>> from datetime import datetime
>>> datetime.fromtimestamp(1483120713887 // 1000)
datetime.datetime(2016, 12, 30, 18, 58, 33)

关于python - 如何用 Python 解析这样的时间 '\/Date(-62135596800000)\/',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41399572/

相关文章:

C# System.DateTime.Now 返回 Windows 中设置的自定义时间格式

mysql - 如何将时间戳附加到 curdate() 返回的 mysql 时间?

php time() 和 microtime() 有时不一致

c++ - 有没有一种标准方法可以在不使用 Boost 的情况下将 std::string 转换为 std::chrono::time_point?

python - 使用 scipy.interpolate.RegularGridInterpolator 出现错误 "There are 100 point arrays, but values has 2 dimensions"

python - 使用 MongoEngine 保存和更新嵌套文档

Python:为两个列表的交集查找相应的索引

python - 在 OS X 10.10 下的 PyCharm 中使用 VirtualBox 设置 Vagrant 时出错

javascript - moment.js - UTC 给出了错误的日期

sql - 在 PostgreSQL 中根据时间更新行