views:

313

answers:

1

I'm trying to convert a datetime object to a UNIX timestamp (preferably in milliseconds, though I wouldn't mind with and without).

Mktime seems to be the method that usually gets it, however I keep getting the error:

AttributeError: type object 'datetime.time' has no attribute 'mktime'.

Can anyone tell me what I'm doing wrong? I keep going round in circles!

+6  A: 

I think you have done

from datetime import datetime, time

instead of

import time
from datetime import datetime

so that the object called time is actually coming from the datetime module, not the time module.

Daniel Roseman
thanks a lot. just another question though.how do i include milliseconds to the return timestamp?
day_trader
`time.mktime(datetimeobject.timetuple()) + datetimeobject.microsecond/1000000.0`
Alex Martelli