0

I tried to convert a string into epoch time but failed.

from datetime import datetime
from dateutil import parser

dt = '2017-01-01 00:00:00'
dt = parser.parse(dt)
print(dt)

print (datetime(dt).timestamp())

Here is error:

 print (datetime(dt).timestamp())
   TypeError: an integer is required (got type datetime.datetime)
Roman
  • 2,587
  • 8
  • 23
  • 51

1 Answers1

0

You only need to use your datetime instance:

print (dt.timestamp())

From the docs:

Return POSIX timestamp corresponding to the datetime instance.

What you are currently doing is passing the parsed datetime object to the datetime constructor (datetime(dt))which expects integers.

Reut Sharabani
  • 29,003
  • 5
  • 68
  • 85