0

I have tried multiple ways like splitting each value and the rearranging. and the code seems too lengthy can someone help me to write it in a shorter way.

time = "28/01/2023 13:17:58" I split it to

lis = [28,01,2023]
lis2 = [13,17,58]

then rearranged it to

t = 28/01/2023 13:17:58

can someone tell me how I can use datetime to simplify this?

Goutham
  • 435
  • 3
  • 16
  • 1
    Did you look at [`datetime.strptime()`](https://docs.python.org/3/library/datetime.html#datetime.datetime.strptime)? – Delgan Feb 16 '23 at 16:21
  • @Delgan tried that but it was throwing error while trying to convert directly from string to datetime – Goutham Feb 16 '23 at 16:23
  • Parsing directive `"%d/%m/%Y %H:%M:%S"` ?! – FObersteiner Feb 16 '23 at 16:37
  • Does this answer your question? [Convert string "Jun 1 2005 1:33PM" into datetime](https://stackoverflow.com/questions/466345/convert-string-jun-1-2005-133pm-into-datetime) – FObersteiner Feb 16 '23 at 16:40

2 Answers2

1
from datetime import datetime

time = "28/01/2023 13:17:58"
datetime_obj = datetime.strptime(time, "%d/%m/%Y %H:%M:%S")
print(datetime_obj) # 2023-01-28 13:17:58

shivankgtm
  • 1,207
  • 1
  • 9
  • 20
1

date = "28/01/2023 13:17:58"

datetime.datetime.strptime(date, '%d/%m/%Y %H:%M:%S')

Output = datetime.datetime(2023, 1, 28, 13, 17, 58)

Abhishek Kumar
  • 768
  • 1
  • 8
  • 23