user17049514
user17049514

Reputation:

What is the format of that datetime?

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 !

Upvotes: 0

Views: 172

Answers (3)

niaei
niaei

Reputation: 2419

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

Upvotes: 1

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

Upvotes: -2

BoarGules
BoarGules

Reputation: 16951

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)))

Upvotes: 3

Related Questions