我得到的回复是时间格式,如
ScheduleDate = "\/Date(1374811200000-0400)\/"
StartTime = "\/Date(-2208931200000-0500)\/"
如何将上述时间转换为
格式"2012-01-01T10:30:00-05:00"
答案 0 :(得分:3)
这就是我想出来的,但是你的示例输入都没有与你的示例输出相匹配,所以我不确定这里是否存在时区偏移错误。
#!/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时代计算负时间增量:
#!/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))