In Python, how do I convert a datetime.datetime
into the kind of float
that I would get from the time.time
function?
Convert from human-readable date to epochlong epoch = new java.text.SimpleDateFormat("MM/dd/yyyy HH:mm:ss").parse("01/01/1970 01:00:00").getTime() / 1000; Timestamp in seconds, remove '/1000' for milliseconds. date +%s -d"Jan 1, 1980 00:00:01" Replace '-d' with '-ud' to input in GMT/UTC time.
Use datetime. strftime(format) to convert a datetime object into a string as per the corresponding format . The format codes are standard directives for mentioning in which format you want to represent datetime. For example, the %d-%m-%Y %H:%M:%S codes convert date to dd-mm-yyyy hh:mm:ss format.
To convert a datetime to a date, you can use the CONVERT() , TRY_CONVERT() , or CAST() function.
It's not hard to use the time tuple method and still retain the microseconds:
>>> t = datetime.datetime.now() >>> t datetime.datetime(2011, 11, 5, 11, 26, 15, 37496) >>> time.mktime(t.timetuple()) + t.microsecond / 1E6 1320517575.037496
time.mktime(dt_obj.timetuple())
Should do the trick.
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