I have a file (JSON file) that contain multiple dates, hours and minutes.

How can I extract the latest date? If the latest date contains 2 "timestamp" how do I use the hours/ minutes in order to extract the latest date?

For example -

1.

01/01/2018 16:23
07/02/2019 16:00

2.

05/02/2018 15:00
05/02/2019 15:05

I want to add the latest date to a file/ list.

1 Answers

0
Community On Best Solutions
import datetime

format_string = "%m/%d/%Y %H:%M"

this is the string used to format the dates. (I assumed you use Month/Day/Year Hour:Minute)

Here is a list with everything you can use.

date_strings = [
 "01/01/2018 16:23",
 "07/02/2019 16:00",
 "05/02/2018 15:00",
 "05/02/2019 15:05"
]

this is the input list you can get it diffrent if you want.

dates = []
for date_string in date_strings:
 date = datetime.datetime.strptime(date_string, format_string)
 dates.append(date)
dates.sort()
dates.reverse()

this will store the dates in date, sort and reverse them (the first is the newest and the last is the oldest).

newest = dates[0].strftime(format_string)
print(newest)

this will print the newest date.

this post covers the same.