pythondatetimeformatarrow-python

Python 3 How to format to yyyy-mm-ddThh:mm:ssZ


I'm new to Python and I cannot for the life of me find my specific answer online. I need to format a timestamp to this exact format to include 'T', 'Z' and no sub or miliseconds like this yyyy-mm-ddThh:mm:ssZ i.e. 2019-03-06T11:22:00Z. There's lots of stuff on parsing this format but nothing about formatting this way. The only way I have nearly got it to work involves sub-seconds which I do not need. I've tried using arrow and reading their documentation but unable to get anything to work. Any help would be appreciated.


Solution

  • Try datetime library

    import datetime
    
    output_date = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%SZ")
    print(output_date)
    

    For more information, refer to the Python Documentation.