Need Help comparing dates

Ben Finney bignose+hates-spam at benfinney.id.au
Fri Jun 16 03:32:45 CEST 2006


Ben Finney <bignose+hates-spam at benfinney.id.au> writes:

> 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.
> 
>     <URL:http://docs.python.org/lib/module-datetime>
> 
> 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

As soon as I sent this, I remembered that the standard library *does*
provide datetime parsing:

    <URL:http://docs.python.org/lib/module-time#l2h-1956>

So your task now consists of:

  - get a date object of today's date using datetime.date.today()
  - define a format for parsing a string date
  - get a struct_time object from time.strptime() feeding it the
    string and the format
  - make any assumptions about missing pieces of the date (e.g. the
    year)
  - get a date object by feeding values to datetime.date()
  - compare the two date objects

-- 
 \     "Hey Homer! You're late for English!"  "Pff! English, who needs |
  `\       that? I'm never going to England!"  -- Barney & Homer, _The |
_o__)                                                        Simpsons_ |
Ben Finney




More information about the Python-list mailing list