0

How to check datetime in python3?

Both date and time are same but in 'b' there is no zero.

  a = '2020-09-08 05:09:02'
  b = '2020-9-8 5:9:2'
  if a == b:
     print("yes")
  else:
     print("no")

 Expected Output:
   yes
  • have a look at the [datetime](https://docs.python.org/3/library/datetime.html) standard lib and especially [strptime](https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior). – FObersteiner Oct 09 '20 at 13:18

1 Answers1

0

you can turn them into datetime objects and compare them

import datetime as dt
a = '2020-09-08 05:09:02'
b = '2020-9-8 5:9:2'
a_dt = dt.datetime.strptime(a, '%Y-%m-%d %H:%M:%S')
b_dt = dt.datetime.strptime(b, '%Y-%m-%d %H:%M:%S')
if a_dt == b_dt:
    print("yes")
else:
    print("no")
Hadrian
  • 917
  • 5
  • 10