如何从日期对象python获取以毫秒(秒后3位小数)为单位的时间值?

Posted 彼岸大师

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何从日期对象python获取以毫秒(秒后3位小数)为单位的时间值?相关的知识,希望对你有一定的参考价值。

要获取具有毫秒(秒后3位小数)的日期字符串,请使用以下命令:

from datetime import datetime

print datetime.utcnow().strftime(%Y-%m-%d %H:%M:%S.%f)[:-3]

 

输出2018-10-04 10:18:32.926 

%f 显示毫秒

 

要在python中获得毫秒,请使用以下代码。

 

import datetime
#  获得当前时间
now=datetime.datetime.now() #2019-04-11 14:18:41.629019
print(now)

# 转换为指定的格式:
otherStyleTime = now.strftime("%Y-%m-%d %H:%M:%S")#2019-04-11 14:18:41
print(otherStyleTime)
#

输出如下所示

  2019-03-11 17:34:28.290409

以上是关于如何从日期对象python获取以毫秒(秒后3位小数)为单位的时间值?的主要内容,如果未能解决你的问题,请参考以下文章