How do I convert from a .NET DateTime to an IronPython datetime?

风格不统一 提交于 2019-12-21 07:25:08

问题


I'm calling an IronPython script and passing it a .NET object that contains a DateTime structure.

I'm trying to use IronPython's JSON support to serialize the object as JSON.

Everything works great until I encounter the .NET DateTime.

How do I convert from the .NET DateTime to the IronPython datetime?


回答1:


Anticipating that people might like to convert between these we actually make it really easy:

import datetime
from System import DateTime
datetime.datetime(DateTime.Now)



回答2:


As we know, the datetime type has the following structure: datetime(year, month, day[, hour[, minute[, second[, microsecond[, tzinfo]]]]]). So all you need is to find a way how to fulfill the required options.

strptime is not yet implemented (otherwise you'd have the ability to simply call datetime.datetime.strptime(DateTime.Now.ToString(format), format).strftime(format)) in IronPython. Instead, you can use the following code (not very optimized one) for now:

from System import DateTime

import datetime

d = DateTime.Now

print datetime.date(d.Year, d.Month, d.Day)
print datetime.datetime(d.Year, d.Month, d.Day, d.Hour, d.Minute, d.Second)


来源:https://stackoverflow.com/questions/6296841/how-do-i-convert-from-a-net-datetime-to-an-ironpython-datetime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!