nick_rinaldi
nick_rinaldi

Reputation: 707

How can I find most recent time from a datetime object in string format

so I have this nested dictionary consisting of either twitter or reddit posts, with each posts id or url as the inner key, and the datetime in string format in the value.

{'reddit': {'my6vne': '2021-04-25 16:00:02',
        'my7uar': '2021-04-25 17:00:02',
        'my9i47': '2021-04-25 18:34:09',
        'myhq5w': '2021-04-26 01:10:37',
        'myioia': '2021-04-26 01:57:58',
        'myiout': '2021-04-26 01:58:25',
        'myjhtj': '2021-04-26 02:38:51',
        'myndga': '2021-04-26 06:16:45',
        'myo6x3': '2021-04-26 07:04:48'},
 'twitter': {'https://twitter.com/twitter/statuses/1386540140068102146': '2021-04-26 '
                                                                     '04:38:24',
         'https://twitter.com/twitter/statuses/1386541987210223618': '2021-04-26 '
                                                                     '04:45:45',
         'https://twitter.com/twitter/statuses/1386542478656827394': '2021-04-26 '
                                                                     '04:47:42',
         'https://twitter.com/twitter/statuses/1386544375086321674': '2021-04-26 '
                                                                     '04:55:14',
         'https://twitter.com/twitter/statuses/1386544515964571648': '2021-04-26 '
                                                                     '04:55:48'}}

Basically, I want to loop through the dictionary, check all the datetime values (which again are strings), and fetch the most recent one, relative to the current time. How would I go about doing this, as the datetime values are strings?

Upvotes: 0

Views: 396

Answers (1)

Rahil Kadakia
Rahil Kadakia

Reputation: 135

Check this out for converting string to a datetime object https://www.educative.io/edpresso/how-to-convert-a-string-to-a-date-in-python

How about you create an object of the current datetime and using the delta method find the difference and choose the minimum value? More here: Finding Min/Max Date with List Comprehension in Python

Upvotes: 1

Related Questions