What is a classy way to way truncate a python datetime object?
In this particular case, to the day. So basically setting hour, minute, seconds, and microseconds to 0.
I would like the output to also be a datetime object, not a string.
What is a classy way to way truncate a python datetime object?
In this particular case, to the day. So basically setting hour, minute, seconds, and microseconds to 0.
I would like the output to also be a datetime object, not a string.
I think this is what you're looking for...
>>> import datetime
>>> dt = datetime.datetime.now()
>>> dt = dt.replace(hour=0, minute=0, second=0, microsecond=0) # Returns a copy
>>> dt
datetime.datetime(2011, 3, 29, 0, 0)
But if you really don't care about the time aspect of things, then you should really only be passing around date objects...
>>> d_truncated = datetime.date(dt.year, dt.month, dt.day)
>>> d_truncated
datetime.date(2011, 3, 29)
更多推荐
所有评论(0)