-2

I have 3 dates (in hours and sec and mins)

2016-11-30T13:27:04-05:00
2016-11-30T13:27:41-05:00
2017-03-01T22:16:35-05:00

How can i get older date which is 2016-11-30T13:27:04-05:00 as output

This python script is not giving correct results

import time
find_a = min(a)
print find_a
Mounarajan
  • 1,357
  • 5
  • 22
  • 43

3 Answers3

2

Try this

dat = ['2016-11-30T13:27:04-05:02', '2016-11-30T13:27:41-05:02','2016-11-30T13:27:04-05:00']
print(min(dat))
Muhammad Usman
  • 10,039
  • 22
  • 39
1

You can use dateutil.parser to parse dates and a min to compare them. Here is an example:

In [1]: from dateutil.parser import parse

In [4]: dates = ['2016-11-30T13:27:04-05:00', '2016-11-30T13:27:41-05:00', '2017-03-01T22:16:35-05:00']
In [5]: min([parse(s) for s in dates])
Out[5]: datetime.datetime(2016, 11, 30, 13, 27, 4, tzinfo=tzoffset(None, -18000))
taras
  • 3,579
  • 3
  • 26
  • 27
1
a = ['2016-11-30T13:27:04-05:00', '2016-11-30T13:27:41-05:00','2017-03-01T22:16:35-05:00'] 
>>> min(a) '2016-11-30T13:27:04-05:00

It works, just try it as normal strings. You can avoid importing time

Do you want to convert them datetime objects and then find the minimum?

use the method specified here https://stackoverflow.com/a/12282040/5334188 to datetime objects and find minimum

be_good_do_good
  • 4,311
  • 3
  • 28
  • 42