Search code examples
pythondatetimeunix-timestamp

Convert string date to unix time


I have problem to convert string of data to unix time

my script:

import datetime


s="2018-06-29 08:15:27"

date_time_obj = datetime.datetime.strptime(s, '%Y-%m-%d %H:%M:%S')

print(type(date_time_obj))

datetime.timestamp(date_time_obj)

and I have this error:

AttributeError: module 'datetime' has no attribute 'timestamp'


Solution

  • timestamp is a method on the datetime class, not the module itself. Just do:

    date_time_obj.timestamp()
    

    Or alternatively:

    datetime.datetime.timestamp(date_time_obj)