Home > Back-end >  What is the format of that datetime?
What is the format of that datetime?

Time:10-13

I have that datetime : 2021-10-12T16:00:00.000 02:00 so I tried that :

import datetime
a = datetime.datetime.strptime("2021-10-12T16:00:00.000 02:00", '%Y-%m-%dT%H:%M:%SZ')

But it does not work I got that

ValueError: time data '2021-10-12T16:00:00.000 02:00' does not match format '%Y-%m-%dT%H:%M:%SZ'

Could you help me please ?

Thank you very much !

CodePudding user response:

This is an ISO date. The easiest way to parse it is to call fromisoformat.

>>> datetime.datetime.fromisoformat("2021-10-12T16:00:00.000 02:00")       
datetime.datetime(2021, 10, 12, 16, 0, tzinfo=datetime.timezone(datetime.timedelta(seconds=7200)))

CodePudding user response:

import datetime
a = datetime.datetime.strptime("2021-10-12T16:00:00.000 02:00", '%Y-%m-%dT%H:%M:%S.%f%z')

You're formatting it wrong.

  1. You have milliseconds too, so there is a %f separated by a .
  2. The time zone is formatted as Z; it has to be %z

CodePudding user response:

from datetime import datetime
today=datetime.now() /// 2021-06-25 07:58:56.550604
dt_string = now.strftime("%d/%m/%Y %H:%M:%S") /// 25/06/2021 07:58:56
  • Related