从 python datetime.datetime 对象中删除毫秒的简单方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31487732/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
simple way to drop milliseconds from python datetime.datetime object
提问by Marc Maxmeister
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:
我的同事需要我从我的 python 时间戳对象中删除毫秒,以符合旧的 POSIX (IEEE Std 1003.1-1988) 标准。为我完成这个任务的折磨路线如下:
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?
有没有比这更简单的方法来结束 mongoDB 的 datetime.datetime 对象?
采纳答案by Anand S Kumar
You can use datetime.replace()
method -
您可以使用datetime.replace()
方法 -
>>> d = datetime.datetime.today().replace(microsecond=0)
>>> d
datetime.datetime(2015, 7, 18, 9, 50, 20)