-2

I have a list of strings that I want to format into a list of dates, before getting the max date time.

The list looks like:

StrList = ['date_range Date Posted: Thu, 08 11 2018 12:12:55 GMT',
 'date_range Date Posted: Thu, 08 11 2018 10:53:49 GMT',
 'date_range Date Posted: Thu, 08 11 2018 09:55:08 GMT',
 'date_range Date Posted: Wed, 07 11 2018 14:23:56 GMT',
 'date_range Date Posted: Wed, 07 11 2018 14:23:12 GMT',
 'date_range Date Posted: Wed, 07 11 2018 09:07:47 GMT',
 'date_range Date Posted: Tue, 06 11 2018 11:44:51 GMT',
 'date_range Date Posted: Mon, 05 11 2018 16:17:51 GMT',
 'date_range Date Posted: Mon, 05 11 2018 14:07:41 GMT',
 'date_range Date Posted: Mon, 05 11 2018 14:03:19 GMT',
 'date_range Date Posted: Mon, 05 11 2018 13:07:10 GMT',
 'date_range Date Posted: Mon, 05 11 2018 13:05:56 GMT',
 'date_range Date Posted: Mon, 05 11 2018 11:41:31 GMT']

I want to get a list that contains only the extracted date and time part of each list element and then subsequently find the maximum of the date times of all the elements.

E.g. Desired output:

 DateList = ['08-11-2018 12:12:55',
 '08-11-2018 10:53:49 GMT',
 '08-11-2018 09:55:08 GMT',
 '07-11-2018 14:23:56 GMT',
 '07-11-2018 14:23:12 GMT',
 '07-11-2018 09:07:47 GMT',
 '06-11-2018 11:44:51 GMT',
 '05-11-2018 16:17:51 GMT',
 '05-11-2018 14:07:41 GMT',
 '05-11-2018 14:03:19 GMT',
 '05-11-2018 13:07:10 GMT',
 '05-11-2018 13:05:56 GMT',
 '05-11-2018 11:41:31 GMT']

Then the final step will be to get the maximum date time which in this case will be:

MaxDate = 08-11-2018 12:12:55

Any help will be appreciated.

Mikee
  • 783
  • 1
  • 6
  • 18
  • Have a look into the `strptime` and `strftime` functions from the `datetime` module. – timgeb Nov 08 '18 at 12:18
  • 1
    Do you need `DateList` for anything else? Or do you feel you need it only to determine `MaxDate`? You need to *parse* the strings into `datetime.datetime()` objects, which are naturally sortable, `max()` on such a list would give you the output in one step. – Martijn Pieters Nov 08 '18 at 12:19
  • 1
    what did you try already? It is always better to help from some starting code – Netwave Nov 08 '18 at 12:19
  • See the duplicate on general datetime parsing advice. Your strings appear structured, so it is trivial to split them all the same way and parse out the datetime portion. You can keep the result as a list for later use, or just to get the max value. It is usually a good idea to convert such strings to datetime objects *as early as possible*, then code everything else against the datetime objects, convert to a string again only when you absolutely need a string. – Martijn Pieters Nov 08 '18 at 12:23

1 Answers1

1

Use datetime.datetime.strptime

from datetime import datetime

str_list = ['date_range Date Posted: Thu, 08 11 2018 12:12:55 GMT',
 'date_range Date Posted: Thu, 08 11 2018 10:53:49 GMT',
 'date_range Date Posted: Thu, 08 11 2018 09:55:08 GMT',
 'date_range Date Posted: Wed, 07 11 2018 14:23:56 GMT',
 'date_range Date Posted: Wed, 07 11 2018 14:23:12 GMT',
 'date_range Date Posted: Wed, 07 11 2018 09:07:47 GMT',
 'date_range Date Posted: Tue, 06 11 2018 11:44:51 GMT',
 'date_range Date Posted: Mon, 05 11 2018 16:17:51 GMT',
 'date_range Date Posted: Mon, 05 11 2018 14:07:41 GMT',
 'date_range Date Posted: Mon, 05 11 2018 14:03:19 GMT',
 'date_range Date Posted: Mon, 05 11 2018 13:07:10 GMT',
 'date_range Date Posted: Mon, 05 11 2018 13:05:56 GMT',
 'date_range Date Posted: Mon, 05 11 2018 11:41:31 GMT']

date_list = [datetime.strptime(s.split(', ')[-1], '%d %m %Y %H:%M:%S %Z') for s in str_list]

which looks like

[datetime.datetime(2018, 11, 8, 12, 12, 55),
 datetime.datetime(2018, 11, 8, 10, 53, 49),
 datetime.datetime(2018, 11, 8, 9, 55, 8),
 datetime.datetime(2018, 11, 7, 14, 23, 56),
 datetime.datetime(2018, 11, 7, 14, 23, 12),
 datetime.datetime(2018, 11, 7, 9, 7, 47),
 datetime.datetime(2018, 11, 6, 11, 44, 51),
 datetime.datetime(2018, 11, 5, 16, 17, 51),
 datetime.datetime(2018, 11, 5, 14, 7, 41),
 datetime.datetime(2018, 11, 5, 14, 3, 19),
 datetime.datetime(2018, 11, 5, 13, 7, 10),
 datetime.datetime(2018, 11, 5, 13, 5, 56),
 datetime.datetime(2018, 11, 5, 11, 41, 31)]

then to get max and min simply:

max_date = max(date_list)

min_date = min(date_list)

outputs

max: datetime.datetime(2018, 11, 8, 12, 12, 55)
min: datetime.datetime(2018, 11, 5, 11, 41, 31)

You can recover a date in the same string form by doing

max_date.strftime('%d %m %Y %H:%M:%D GMT')

outputs

'08 11 2018 12:12:55 GMT'
FHTMitchell
  • 11,793
  • 2
  • 35
  • 47