pythondatetimetimestamp

How to set a specific future datetime timestamp


I would like to know how to set a specific DateTime timestamp using the DateTime module in python. For example if I want a specific event to be triggered on 1 August 2022. How can I make a DateTime timestamp that will hold that date?


Solution

  • You can construct a new datetime object with your date (and optionally with time) and then call timestamp() on it. For example, you can do:

    from datetime import datetime
    
    timestamp = datetime(2022, 8, 1).timestamp()
    # timestamp is now 1659304800.0
    

    You can find the official documentation here.