Subtract n months from datetime
Cameron Simpson
cs at cskk.id.au
Tue Jun 21 17:55:29 EDT 2022
On 21Jun2022 17:02, Paulo da Silva <p_d_a_s_i_l_v_a_ns at nonetnoaddress.pt> wrote:
>I have a datetime, not a date.
Then you need a date. I would break the datetime into a date and a time,
then do the months stuff to the date, then compose a new datetime from
the result.
>Anyway, the use of calendar.monthrange simplifies the task a lot.
Hmm, yes it would.
The important thing to remember about any solutions mentioned is that
dates and datetimes have different semantics. Specificly, you can't add
fixed elapsed times such as seconds to do "calendar like" arithmetic,
which works in days etc because months have varying numbers of days, and
days have varying numbers of seconds (not merely the odd leap second but
also the horrors of timezones and summer/winter time shifts).
So working with the calendar component (days upwards) is a meaningful
thing. But working in, say, seconds with the _bjective_ of doing days or
months is nearly pointless.
Cheers,
Cameron Simpson <cs at cskk.id.au>
More information about the Python-list
mailing list