simple way to drop milliseconds from python datetime.datetime object [duplicate] Ask Question

simple way to drop milliseconds from python datetime.datetime object [duplicate] Ask Question

My colleague needs me to drop the milliseconds from my python timestamp objects in order to comply with the old POSIX (IEEE Std 1003.1-1988) standard. The tortured route that accomplishes this task for me is as follows:

datetime.datetime.strptime(datetime.datetime.today().strftime("%Y-%m-%d %H:%M:%S"),"%Y-%m-%d %H:%M:%S")

Is there a simpler way to end up with a datetime.datetime object for mongoDB than this?

ベストアンサー1

You can use datetime.replace() method -

>>> d = datetime.datetime.today().replace(microsecond=0)
>>> d
datetime.datetime(2015, 7, 18, 9, 50, 20)

おすすめ記事