Home > Back-end >  "07:37 am GMT 5.30 on 10/11/2011" how to convert this string to datetime in python
"07:37 am GMT 5.30 on 10/11/2011" how to convert this string to datetime in python

Time:10-13

According to the questions, it is been asked to remove the 5.30 from the string:

import datetime
b="07:37 am GMT 5.30 on 10/11/2011"
a=b.replace(" 5.30","")
print(a)

Now, how to covert this string to date using datetime in python? I tried :

c=datetime.datetime("%H:%M %p on %d/%m/%Y")
Print(c)

But this didn't work, it shows error. What to do?

CodePudding user response:

Based on your code, you may use strptime method to convert string into datetime objects.

import datetime 
b = "07:37 am GMT 5.30 on 10/11/2011"
a = b.replace(" 5.30","")
a = datetime.datetime.strptime(a, "%I:%M %p GMT on %d/%m/%Y")
print(a)
  • Related