-2

How can I transform a string datetime like this:

"2018-07-13T00:00:00+00:00"

Into this?

"2018-07-13T00:00:00.000000+00:00"
FObersteiner
  • 22,500
  • 8
  • 42
  • 72
sg_sg94
  • 2,248
  • 4
  • 28
  • 56
  • 2
    What have you tried so far? – Klaus D. Sep 29 '21 at 09:20
  • You'll find this helpful for when you start writing your code:- https://docs.python.org/3/library/datetime.html –  Sep 29 '21 at 09:33
  • `datetime.fromisoformat("2018-07-13T00:00:00+00:00").isoformat(timespec="microseconds")`? – FObersteiner Sep 29 '21 at 09:37
  • See [Format a datetime into a string with milliseconds](https://stackoverflow.com/q/7588511/2745495) and [Python: print the time zone from strftime](https://stackoverflow.com/q/31299580/2745495). (It seems to be a combo duplicate of both questions.) – Gino Mempin Sep 29 '21 at 09:39

1 Answers1

0

try this out:

from datetime import datetime

datetime_string = "2018-07-13T00:00:00+00:00"
new_datetime_string = datetime.fromisoformat(datetime_string).isoformat(timespec="microseconds")

and for before python V3.7:

date_time = datetime.strptime(datetime_string, '%Y-%m-%dT%H:%M:%S%z').strftime("%Y-%m-%dT%H:%M:%S.%f%z")
hmn Falahi
  • 730
  • 5
  • 22