Am I asking too much from datetime?

Yermat loic at fejoz.net
Tue Jun 1 07:40:44 EDT 2004


thehaas at binary.net wrote:
> I'm trying to use the datetime module.  The following seems to work
> any day except the first day of the month:
> 
> 
>>>>import datetime
>>>>today = datetime.date.today()
>>>>print today
> 
> 2004-06-01
> 
>>>>yesterday = datetime.date(today.year, today.month, today.day-1)
> 
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> ValueError: day is out of range for month
> 
> In other languages (specifically Java, but I'm sure these are others),
> this would come out as "2004-05-31".
> 
> Is this is a bug, or outside of the functionality of datetime?
> 
> 
> -- 
> Mike Hostetler          
> thehaas at binary.net 
> http://www.binary.net/thehaas 

Of course it does not work !
0 is not a valid day...
You should do something like:

 >>> import datetime
 >>> today = datetime.date.today()
 >>> print today
2004-06-01
 >>> oneDay = datetime.timedelta(days=1)
 >>> print oneDay
1 day, 0:00:00
 >>> print today-oneDay
2004-05-31


-- 
Yermat




More information about the Python-list mailing list