On Wed, 2006-02-22 at 19:12 -0800, Luke Skibinski Holt wrote:
> Is there a means of getting a datetime object as an offset from the
> current date?
> I need to get the datetime objects for the coming weekend, but if my
> result is greater than the number of days in a month it fails rather
> than wrapping into the next month. mx.DateTime and datetime.datetime
> don't seem to support any such thing - has anybody else done such a
> thing or could recommend a fix?
>
> dt = datetime.now()
> dt.replace(day=28, month=2)
> wd = dt.weekday()
> sat = dt.replace(day=dt.day+(5-wd))
> sun = dt.replace(day=dt.day+(6-wd))
>
> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> ValueError: day is out of range for month
Probably more of a question for comp.lang.python, but the answer is
pretty simple: import timedelta from datetime as well and then use
sat = dt + timedelta(days = 5 - dt.weekday())
This handles month and year wrapping without any problem.
Cheers,
Malcolm
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---