Need Help comparing dates

Ben Finney bignose+hates-spam at benfinney.id.au
Thu Jun 15 21:23:56 EDT 2006


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, largely
because there's no "one obvious way to do it". An existing PEP
proposing adding such functionality has since been withdrawn:

    <URL:http://www.python.org/dev/peps/pep-0321/>

Parsing datetime values from strings is fuzzy and prone to lots of
assumptions and errors, so the programmer must choose their own set of
assumptions. One popular implementation of a specific set of
assumptions is the egenix 'mxDateTime' module.

    <URL:http://www.egenix.com/files/python/mxDateTime.html>

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.

-- 
 \           "There is no reason anyone would want a computer in their |
  `\          home."  -- Ken Olson, president, chairman and founder of |
_o__)                                    Digital Equipment Corp., 1977 |
Ben Finney




More information about the Python-list mailing list