Im trying to convert a datetime object to a string but it does not seem to give me the desired output.
from datetime import datetime
cur_time = datetime.now()
last_runtime = cur_time.strftime("%Y-%m-%dT%H:%M:%S.%f %Z")
print(last_runtime)
My current output:
2021-10-13T09:09:27.824592
My desired output:
2021-10-13T09:09:27.825 00:00
CodePudding user response:
You can use .astimezone()
and small z on the format string:
from datetime import datetime
cur_time = datetime.now()
last_runtime = cur_time.astimezone().strftime("%Y-%m-%dT%H:%M:%S.%f%z")
last_runtime = "{0}:{1}".format(
last_runtime[:-2],
last_runtime[-2:]
)
print(last_runtime)