How to print current GMT time in a specific format in python

19,085

https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior

Python uses the above linked directives to format time.

So, for example, if you try something like:

import datetime  

datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%dT%H:%M:%S.%f%Z")

This should display the format you mentioned in the right way

Share:
19,085
tsaebeht
Author by

tsaebeht

Updated on June 09, 2022

Comments

  • tsaebeht
    tsaebeht almost 2 years

    The format I have been given yyyy-MM-dd'T'HH:mm:ss.SSS'Z'. However, this isn't working with my code which is

    datetime.utcnow().strftime("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
    

    The output is simply:

    yyyy-MM-dd'T'HH:mm:ss.SSS'Z'

    Why does this not work? What is the fix ?

  • gzerone
    gzerone over 3 years
    datetime.utcnow() :)
  • saurabh gupta
    saurabh gupta almost 3 years
    @gzerone you are an angel.