0

How can i get datetime in ISO 8601 date format (YYYY-MM-DDThh:mmTZD). Example 2019-02-26T09:30:46+03:00

I tried using

from datetime import datetime
d = datetime.now()
d.isoformat()

But the output is now correct

'2020-07-29T15:47:46.974744'

Emmanuel Mtali
  • 4,383
  • 3
  • 27
  • 53
  • 3
    What is the problem? – TheOneMusic Jul 29 '20 at 12:56
  • Does this answer your question? [How do you convert YYYY-MM-DDTHH:mm:ss.000Z time format to MM/DD/YYYY time format in Python?](https://stackoverflow.com/questions/214777/how-do-you-convert-yyyy-mm-ddthhmmss-000z-time-format-to-mm-dd-yyyy-time-forma) – Sachin Yadav Jul 29 '20 at 13:01
  • The format is different, notice +xx:xx at the end also I dont need milliseconds -> [.974744]. I need something like 2019-02-26T09:30:46+03:00 – Emmanuel Mtali Jul 29 '20 at 13:02

1 Answers1

2
d = datetime.now()
d.strftime("%Y-%m-%dT%H:%M:%S%z")

Do note that %z will return empty if its a naive datetime object

This should give you the format you're looking for. https://strftime.org/ This is a good reference on the available formats

Joe Tan
  • 116
  • 3