Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

datetime.timedelta.replace?

17 views
Skip to first unread message

Skip Montanaro

unread,
Oct 9, 2013, 11:15:49 AM10/9/13
to Python
Datetime objects have a replace method, but timedelta objects don't.
If I take the diff of two datetimes and want to zero out the
microseconds field, is there some way to do it more cleanly than this?

delta = dt1 - dt2
zero_delta = datetime.timedelta(days=delta.days, seconds=delta.seconds)

I guess that's not bad, but replace() seems cleaner (or at least more
congruent with datetime objects).

Skip

Joshua Landau

unread,
Oct 11, 2013, 1:56:18 PM10/11/13
to Skip Montanaro, Python
Maybe one of

delta - datetime.timedelta(0, 0, delta.microseconds)

or

delta - delta % datetime.timedelta(seconds=1)

is clearer.
0 new messages