Need Help comparing dates
sjmachin at lexicon.net
Fri Jun 16 03:43:01 CEST 2006
On 16/06/2006 11:23 AM, Ben Finney wrote:
> colincolehour at gmail.com writes:
>> I am new to Python and am working on my first program. I am trying
>> to compare a date I found on a website to todays date. The problem I
>> have is the website only shows 3 letter month name and the date.
>> Example: Jun 15
> The 'datetime' module in the standard library will do the job of
> creating date objects that can be compared.
> Construct a date from arbitrary values with datetime.date(), get the
> current date with datetime.date.today(). The objects returned by those
> functions can be compared directly.
> As for how to get from a string representation to a date object,
> you're now talking about parsing strings to extract date/time
> information. This isn't provided in the standard library,
time.strptime() doesn't do "parsing strings to extract date/time
It appears to me to do a reasonably tolerant job on the OP's spec i.e.
month abbreviation and a day number:
|>> import time
|>> time.strptime('Jun 15', '%b %d')
(1900, 6, 15, 0, 0, 0, 4, 166, -1)
|>> time.strptime('dec 9', '%b %d')
(1900, 12, 9, 0, 0, 0, 4, 152, -1)
The OP should be able to use that to get the month and the day. The year
of the web page date will need some guessing (e.g. is within the last
year) and not even Python has a crystal ball :-)
BTW, datetime has grown a strptime in version 2.5.
> That module predates (and was largely an inspiration for) the standard
> library 'datetime' module; however, I don't know if the egenix module
> uses objects that can be used with those from the standard library.
More information about the Python-list