Literally the inverse of this question, is there an easy way to get a .Net DateTime
from an IronPython datetime
?
Clearly, one could
DateTime
constructorbut those are both messy. This doesn't work either:
pydate = datetime.datetime.now()
csharp = DateTime(pydate) # Crashes, because .Net wants a 'long' for Ticks
Is there an easy cast or a short way to get the Ticks
that .Net wants?
I was fairly certain a direct conversion was already allowed, but I was wrong. I added it in 31f5c88 but that won't be available until (currently unscheduled) 2.7.6.
In the meantime the best way would be to use the timetuple
method to get the parts:
dt = datetime.now()
d = DateTime(*dt.timetuple()[:6])
# For UTC times, you need to pass 'kind' as a kwarg
# because of Python's rules around using * unpacking
udt = datetime.now()
ud = DateTime(*udt.timetuple()[:6], kind=DateTimeKind.Utc)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With