0

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.

gadp
  • 5
  • 2
  • Welcome! To ask [On Topic question](https://stackoverflow.com/help/on-topic) , please read [Question Check list](https://meta.stackoverflow.com/questions/260648/stack-overflow-question-checklis)t and the [perfect question](http://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/) and how to create a [Minimal, Complete and Verifiable Example](https://stackoverflow.com/help/mcve) and take the [tour](https://stackoverflow.com/tour) . **We are very willing to help you fix your code, but we dont write code for you**. – dr0i May 12 '19 at 10:12
  • Possible duplicate of [Find oldest/youngest datetime object in a list](https://stackoverflow.com/questions/3922644/find-oldest-youngest-datetime-object-in-a-list) – sahasrara62 May 12 '19 at 10:21

1 Answers1

0
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.

MaxSilvester
  • 193
  • 1
  • 12