0

I have strings in following format:

Friday January 3 2020 16:40:57
Thursday January 2 2020 19:26:19
Sunday January 5 2020 01:24:55
Tuesday December 31 2019 17:31:42

What is the best way to convert them into python date and time?

ggorlen
  • 44,755
  • 7
  • 76
  • 106
Rana
  • 389
  • 5
  • 17
  • 2
    Say more. Are the strings just stand alone like that or part of a larger string? – dawg Jan 11 '20 at 20:40
  • _What is the best way to convert them into python date and time?_ What is the issue, exactly? There are already tons of answers to that question available. – AMC Jan 11 '20 at 20:43
  • 2
    Does this answer your question? [Converting string into datetime](https://stackoverflow.com/questions/466345/converting-string-into-datetime) – AMC Jan 11 '20 at 20:44
  • 2
    I do not think this question adds to the body of knowledge on Python date conversion. There are literally hundreds of duplicates... – dawg Jan 11 '20 at 20:47

2 Answers2

2

You can use datetime.strptime:

from datetime import datetime

d = "Friday January 3 2020 16:40:57"
datetime_object = datetime.strptime(d, '%A %B %d %Y %H:%M:%S')

print(datetime_object)
Sebastian Brosch
  • 42,106
  • 15
  • 72
  • 87
0

You can use dateparser

Install:

$ pip install dateparser

Sample Code:

import dateparser

t1 = 'Friday January 3 2020 16:40:57'
t2 = 'Thursday January 2 2020 19:26:19'
t3 = 'Sunday January 5 2020 01:24:55'
t4 = 'Tuesday December 31 2019 17:31:42'

dt1 = dateparser.parse(t1)
dt2 = dateparser.parse(t2)
dt3 = dateparser.parse(t3)
dt4 = dateparser.parse(t4)

for dt in [dt1, dt2, dt3, dt4]:
    print(dt)

Output:

2020-01-03 16:40:57
2020-01-02 19:26:19
2020-01-05 01:24:55
2019-12-31 17:31:42
Bilesh Ganguly
  • 3,792
  • 3
  • 36
  • 58