I can create a similar Date object in Java by java.util.Date(milliseconds). How do I create the comparable in Python?
+5
A:
Just convert it to timestamp
datetime.datetime.fromtimestamp(ms//1000)
vartec
2009-04-14 17:12:51
A note -- in Python 3, (/) will perform floating-point division. To perform integral division, use (//).
John Millikin
2009-04-14 17:37:31
Don't you actually want float division anyway? Otherwise you're losing any precision below 1 second (held in the fractional part of the timestamp). Better to use ms/1000.0 with no truncation.
Brian
2009-04-14 19:45:57
Not all platforms support fractional timestamps -- better to stick with POSIX's integers.
John Millikin
2009-04-14 20:28:18
Surely datetime always supports it though? If we've got sub-second precision, it seems wrong to throw it away. If we don't there's no harm done - we just retain that original precision.
Brian
2009-04-14 21:00:52