0

I am new to Python, I have list l with 8th element like this, I want this to be compared with, Coding like this. Can some body let me know, is this right way of comparing string to time format?

>>> l[7]
'07APR2015'
>>> import time
>>> td= time.strftime('%Y%m%d')
>>> td
'20170227'
>>> l[7]>td
False
>>> l[7]<td
True
>>> tt ='02APR2015'
>>> tt > td
False
>>> tt < td
True

If not, please suggest me in conversion.

Thanks for the help.

subro
  • 673
  • 1
  • 10
  • 23

1 Answers1

1

as say in the comment you need to convert the string to a datetime

take a look at this question , you have an exemple how to convert date

a solution can be (in python 3) :

>>> from datetime import datetime
>>> datetime_object = datetime.strptime('07APR2015', '%d%b%Y')                   
>>> datetime_object
datetime.datetime(2015, 4, 7, 0, 0)
>>> now = datetime.now()                                      
>>> now < datetime_object
False
>>> now > datetime_object
True
Community
  • 1
  • 1
jmny
  • 310
  • 2
  • 15
  • This is helpful, thank you, but now is giving me seconds also, which i dont need, I would like to compare with date part of now, can you help me with that? Thanks ! again – subro Feb 27 '17 at 15:21
  • You can get the date object from a datetime : http://stackoverflow.com/questions/3743222/how-do-i-convert-datetime-to-date-in-python – jmny Feb 27 '17 at 15:25
  • Thank you, This really helps – subro Feb 27 '17 at 15:27