0

If I have a datetime object like

datetime.datetime(2016,4,5,10,4,7,000)

How would I convert it to unix timestamp with millisecond accuracy?

I am using python version 2.7.10. The solution here (How can I convert a datetime object to milliseconds since epoch (unix time) in Python?) using total_seconds seems to round to the nearest second on my system.

Community
  • 1
  • 1
sakurashinken
  • 3,940
  • 8
  • 34
  • 67
  • It doesn't "round", it uses a function whose resolution is whole seconds. You can extract the `microseconds` field from the `timedelta` object and add it as an additional step. – tripleee Jul 01 '16 at 06:16

2 Answers2

2

Use the timestamp() method.

>>> import datetime
>>> datetime.datetime(2016,4,5,10,4,7,000).timestamp()
1459875847.0
>>> datetime.datetime(2016,4,5,10,4,7,123456).timestamp()
1459875847.123456
TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
0

If you dont mind an inefficient solution then you can change system date and time, check this for reference and then get the time stamp using time.time().

Better methods must exist, its just a work around.

Community
  • 1
  • 1
shiva
  • 2,535
  • 2
  • 18
  • 32