Converting a datetime object to an integer python
Question: I would like to convert a datetime object to an int in python:

    
    
    import datetime
    
    time_entered = datetime.datetime.strptime(raw_input("Time1: "), "%H%M")
    time_left = datetime.datetime.strptime(raw_input("Time2"), "%H%M")
    
    time_taken = time_left - time_entered
    
    int(time_taken)
    

When I run that code I get the following error:

> TypeError: int() argument must be a string or a number, not
> 'datetime.timedelta'


Answer: You can convert datetime object to timetuple, and then use time.mktime
function

    
    
    import time
    from datetime import datetime
    timestamp = int(time.mktime(datetime.now().timetuple())
    

Then you can convert it back:

    
    
    now = datetime.fromtimestamp(timestamp)
    

