将 ASP.Net JSON 日期转换为 Python 日期时间 [英] Converting ASP.Net JSON Date into Python datetime
本文介绍了将 ASP.Net JSON 日期转换为 Python 日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我从其他人那里得到的回复是一种类似的时间格式
I am getting a response from the rest is an time format like
ScheduleDate = "\/Date(1374811200000-0400)\/"
StartTime = "\/Date(-2208931200000-0500)\/"
我如何将上述时间转换为像
How could I convert the above time to format like
"2012-01-01T10:30:00-05:00"
推荐答案
这是我想出来的,但是您的示例输入都与您的示例输出不匹配,所以我不确定这里是否存在时区偏移错误与否.
This is what I came up with, but neither of your example inputs matched up to your example output so I'm not sure whether there's a timezone offset error here or not.
#!/usr/bin/env python
import datetime
def parse_date(datestring):
timepart = datestring.split('(')[1].split(')')[0]
milliseconds = int(timepart[:-5])
hours = int(timepart[-5:]) / 100
time = milliseconds / 1000
dt = datetime.datetime.utcfromtimestamp(time + hours * 3600)
return dt.strftime("%Y-%m-%dT%H:%M:%S") + '%02d:00' % hours
ScheduleDate = "\/Date(1374811200000-0400)\/"
StartTime = "\/Date(-2208931200000-0500)\/"
print(parse_date(ScheduleDate))
print(parse_date(StartTime))
Windows 似乎不喜欢 datetime.(utc)?fromtimestamp()
中的负值.可以要求它计算 Unix 纪元的负时间增量:
It seems to be the case that Windows doesn't like negative values in datetime.(utc)?fromtimestamp()
. It may be possible to ask it to compute a negative time delta from the Unix epoch:
#!/usr/bin/env python
import datetime
EPOCH = datetime.datetime.utcfromtimestamp(0)
def parse_date(datestring):
timepart = datestring.split('(')[1].split(')')[0]
milliseconds = int(timepart[:-5])
hours = int(timepart[-5:]) / 100
adjustedseconds = milliseconds / 1000 + hours * 3600
dt = EPOCH + datetime.timedelta(seconds=adjustedseconds)
return dt.strftime("%Y-%m-%dT%H:%M:%S") + '%02d:00' % hours
ScheduleDate = "\/Date(1374811200000-0400)\/"
StartTime = "\/Date(-2208931200000-0500)\/"
print(parse_date(ScheduleDate))
print(parse_date(StartTime))
这篇关于将 ASP.Net JSON 日期转换为 Python 日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文