本文介绍了从python datetime.datetime对象删除毫秒的简单方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的同事需要我从python时间戳对象中删除毫秒,以便符合旧的POSIX(IEEE Std 1003.1-1988)标准。为我完成此任务的受折磨路线如下:

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")

有没有比mongoDB的datetime.datetime对象更简单的方法了?

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

推荐答案

您可以使用方法-

You can use datetime.replace() method -

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

这篇关于从python datetime.datetime对象删除毫秒的简单方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 23:08